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 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
|
/*
* Copyright (c) 2001 by J. R. Westmoreland <jr@jrw.org>
* Portions Copyright (c) 2002-2004 by Matthew Palmer <mpalmer@debian.org>
*
* Original module/version: mod_auth_mysql v2.20
* Originally written and maintained by Zeev Suraski <bourbon@netvision.net.il>
* A couple of fixes by Marschall Peter <Peter.Marschall@gedos.de>
* and Brent Metz <bmetz@thor.tjhsst.edu>
* MySQL/PHP style MD5 hashes, and an integration with the mod-auth-mysql
* maintained by Bill Joned by Matthew Palmer <mpalmer@debian.org>
*
* This version maintained by Matthew Palmer <mpalmer@debian.org>
*
* Please read the INSTALL and USAGE files for further information.
*
* 2004-02-01 MURAKAMI, takeshi <takeshi@softagency.co.jp>
* add port, socket
* 2004-02-07 MURAKAMI, takeshi <takeshi@softagency.co.jp>
* apache2
* 2004-09-20 Joseph Walton <joe@kafsemo.org>
* SHA1 hash support
*/
#define AUTH_MYSQL_VERSION "4.3.9"
#include "config.h"
#ifdef APACHE2
#define PALLOC apr_palloc
#define PCALLOC apr_pcalloc
#define SNPRINTF apr_snprintf
#define PSTRDUP apr_pstrdup
#define PSTRCAT apr_pstrcat
#define APACHELOG(severity, handle, message...) ap_log_error(APLOG_MARK, APLOG_NOERRNO | severity, 0, handle->server, message)
#else
#define PALLOC ap_palloc
#define PCALLOC ap_pcalloc
#define SNPRINTF ap_snprintf
#define PSTRDUP ap_pstrdup
#define PSTRCAT ap_pstrcat
#define APACHELOG(severity, handle, message...) ap_log_error(APLOG_MARK, APLOG_NOERRNO | severity, handle->server, message)
#endif
#include <httpd.h>
#include <http_config.h>
#include <http_core.h>
#include <http_protocol.h>
#include <http_log.h>
#ifdef APACHE2
#include "http_request.h" /* for ap_hook_(check_user_id | auth_checker)*/
#include <apr_md5.h>
#include <apr_sha1.h>
#else
#include <ap_md5.h>
#include <ap_sha1.h>
#endif
#include <mysql.h>
#include <errmsg.h>
#include <mysqld_error.h>
#ifdef HAVE_CRYPT_H
#include <crypt.h>
#endif
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
/* This are the system-wide config options; the more specific options live in
* a mysql_auth_config_rec structure, one for each MySQL-configured directory.
*/
static char *auth_db_host = NULL,
*auth_db_name = NULL,
*auth_db_user = NULL,
*auth_db_pwd = NULL;
static int auth_db_override = 1;
char *tmp_host = NULL;
char *auth_db_socket = NULL;
long auth_db_port = -1;
unsigned long auth_db_client_flag = 0;
/* Support for general-purpose encryption schemes. Should be fairly straightforward.
* We have a checking routine and a name for it (for AuthMySQL_Encryption_Types).
*/
#define PLAINTEXT_ENCRYPTION_FLAG 1<<0
#ifdef CRYPT_DES
#define CRYPT_DES_ENCRYPTION_FLAG 1<<1
#endif
#define MYSQL_ENCRYPTION_FLAG 1<<2
#ifdef CRYPT_MD5
#define CRYPT_MD5_ENCRYPTION_FLAG 1<<3
#endif
#define PHP_MD5_ENCRYPTION_FLAG 1<<4
#ifdef HAVE_CRYPT_H
#define CRYPT_ENCRYPTION_FLAG 1<<5
#endif
#define SHA1SUM_ENCRYPTION_FLAG 1<<6
static int check_no_encryption(const char *passwd, char *enc_passwd)
{
return (!strcmp(passwd, enc_passwd));
}
#ifdef CRYPT_DES
static int check_crypt_des_encryption(const char *passwd, char *enc_passwd)
{
/* Ensure that MD5 passwords aren't checked here */
if (!strncmp(enc_passwd, "$1$", 3)) {
return 0;
}
return (!strcmp(crypt(passwd, enc_passwd), enc_passwd));
}
#endif
#ifdef CRYPT_MD5
static int check_crypt_MD5_encryption(const char *passwd, char *enc_passwd)
{
/* Make sure only MD5 passwords are checked */
if (strncmp(enc_passwd, "$1$", 3)) {
return 0;
}
return (!strcmp(crypt(passwd, enc_passwd), enc_passwd));
}
#endif
#ifdef HAVE_CRYPT_H
static int check_crypt_encryption(const char *passwd, char *enc_passwd)
{
return (!strcmp(crypt(passwd, enc_passwd), enc_passwd));
}
#endif
char hex_digit(char c)
{
if (c < 10) {
return c+'0';
} else {
return c-10+'a';
}
}
static char *md5_hex_hash(const char *pass)
{
unsigned char hash[16];
/* This makes this function *very* specialised. Change this to
* use dynamic memory if you want to reuse it somewhere else */
static char real_hash[33];
int i;
#ifdef APACHE2
apr_md5_ctx_t ct;
apr_md5_init(&ct);
apr_md5_update(&ct, pass, strlen(pass));
apr_md5_final(hash, &ct);
#else
AP_MD5_CTX ct;
ap_MD5Init(&ct);
ap_MD5Update(&ct, pass, strlen(pass));
ap_MD5Final(hash, &ct);
#endif
/* Now we convert the 16 octet hash to a 32 byte hex string */
for (i = 0; i < 16; i++) {
real_hash[2*i+1] = hash[i] & 0xF;
real_hash[2*i] = (hash[i] & 0xF0) >> 4;
}
for (i = 0; i < 32; i++) {
real_hash[i] = hex_digit(real_hash[i]);
}
real_hash[32] = '\0';
return real_hash;
}
static int check_PHP_MD5_encryption(const char *passwd, char *enc_passwd)
{
return (!strcmp(md5_hex_hash(passwd), enc_passwd));
}
static char *sha1_hex_hash(const char *passwd)
{
int i;
#ifdef APACHE2
apr_sha1_ctx_t ct;
char hash[APR_SHA1_DIGESTSIZE];
static char real_hash[APR_SHA1_DIGESTSIZE * 2 + 1];
apr_sha1_init(&ct);
apr_sha1_update(&ct, passwd, strlen(passwd));
apr_sha1_final(hash, &ct);
#else
AP_SHA1_CTX ct;
char hash[SHA_DIGESTSIZE];
static char real_hash[SHA_DIGESTSIZE * 2 + 1];
ap_SHA1Init(&ct);
ap_SHA1Update(&ct, passwd, strlen(passwd));
ap_SHA1Final(hash, &ct);
#endif
/* Now we convert the 20 octet hash to a 40 byte hex string */
for (i = 0; i < sizeof(hash); i++) {
real_hash[2*i+1] = hash[i] & 0xF;
real_hash[2*i] = (hash[i] & 0xF0) >> 4;
}
for (i = 0; i < sizeof(real_hash); i++) {
real_hash[i] = hex_digit(real_hash[i]);
}
real_hash[sizeof(real_hash)-1] = '\0';
return real_hash;
}
static int check_SHA1Sum_encryption(const char *passwd, char *enc_passwd)
{
return (!strcmp(sha1_hex_hash(passwd), enc_passwd));
}
static int check_mysql_encryption(const char *passwd, char *enc_passwd)
{
char scrambled_passwd[32];
make_scrambled_password(scrambled_passwd, passwd);
return (!strcmp(scrambled_passwd, enc_passwd));
}
typedef struct {
char *name;
int (*check_function)(const char *passwd, char *enc_passwd);
int flag;
} encryption_type_entry;
encryption_type_entry supported_encryption_types[] = {
{ "Plaintext", check_no_encryption, PLAINTEXT_ENCRYPTION_FLAG },
#if CRYPT_DES
{ "Crypt_DES", check_crypt_des_encryption, CRYPT_DES_ENCRYPTION_FLAG },
#endif
{ "MySQL", check_mysql_encryption, MYSQL_ENCRYPTION_FLAG },
#if CRYPT_MD5
{ "Crypt_MD5", check_crypt_MD5_encryption, CRYPT_MD5_ENCRYPTION_FLAG },
#endif
{ "Crypt", check_crypt_encryption, CRYPT_ENCRYPTION_FLAG },
{ "PHP_MD5", check_PHP_MD5_encryption, PHP_MD5_ENCRYPTION_FLAG },
{ "SHA1Sum", check_SHA1Sum_encryption, SHA1SUM_ENCRYPTION_FLAG},
/* add additional encryption types below */
{ NULL, NULL, 0 }
};
static int get_encryption_flag(const char *name)
{
register encryption_type_entry *ete=supported_encryption_types;
while (ete->name) {
if (!strcmp(ete->name, name)) {
return ete->flag;
}
ete++;
}
return 0;
}
/* end of support for general-purpose encryption schemes */
/* Per-directory configuration structure. One of these is created for each
* <Directory>...</Directory> and .htaccess file which requests authentication
*/
typedef struct {
char *dir;
char *db_host;
char *db_socket;
unsigned int db_port;
char *db_user;
char *db_pwd;
char *db_name;
MYSQL *dbh;
/* Boolean options */
unsigned char persistent;
unsigned char enable_mysql_auth;
/* Some MySQL errors are retryable; if we retry the operation
* by recursing into the same function, we set this so we don't
* recurse indefinitely if it's a permanent error.
*/
unsigned char dbh_error_lastchance;
char *user_table;
char *group_table;
char *user_field;
char *password_field;
char *group_field;
char *group_user_field;
char *group_where_clause;
char *password_where_clause;
int encryption_types;
unsigned char using_encryption_types;
unsigned char allow_empty_passwords;
unsigned char authoritative;
/* You're not going to believe this, but, near as I can tell, apache
* doesn't respect the last part of the config_rec. May be an
* underflow in some code somewhere, but I'm not taking no chances
* with *my* config variables...
*/
char sacrificial_lamb[15];
} mysql_auth_config_rec;
module auth_mysql_module;
#ifdef APACHE2
static apr_status_t
#else
static void
#endif
auth_mysql_cleanup(void *ptr)
{
mysql_auth_config_rec *sec = ptr;
if (sec->dbh) {
#ifdef DEBUG
syslog(LOG_DEBUG, "MAMDEBUG: Closing MySQL connection");
#endif
mysql_close(sec->dbh);
sec->dbh = NULL;
}
}
/* Do the magic required when the module is first loaded.
*/
#ifdef APACHE2
void mysql_auth_init_handler(server_rec *s, apr_pool_t *p)
#else
void mysql_auth_init_handler(server_rec *s, pool *p)
#endif
{
#ifdef APACHE2
#else
#if MODULE_MAGIC_NUMBER >= 19980527
ap_add_version_component("AuthMySQL/" AUTH_MYSQL_VERSION);
#endif
#endif
}
/* Called each and every time a new per-directory configuration is
* created. We just initialise variables and set defaults. This is
* run *before* actual config takes place.
*/
#ifdef APACHE2
void *create_mysql_auth_dir_config(apr_pool_t *p, char *d)
#else
void *create_mysql_auth_dir_config(pool *p, char *d)
#endif
{
#ifdef DEBUG
int i;
#endif
mysql_auth_config_rec *sec = (mysql_auth_config_rec *) PCALLOC(p, sizeof(mysql_auth_config_rec));
#ifdef DEBUG
syslog(LOG_DEBUG, "MAMDEBUG: Now configuring server config for %s", d);
syslog(LOG_DEBUG, "MAMDEBUG: sizeof(mysql_auth_config_rec) = %i",
sizeof(mysql_auth_config_rec));
#endif
sec->db_name = sec->db_socket = sec->db_user = sec->db_pwd = NULL;
sec->dbh = NULL;
/* When the memory for this connection record is cleaned, we must
* be sure to close the DB connection, if it exists. If this does
* not happen, we are in a world of pain.
*/
#ifdef APACHE2
apr_pool_cleanup_register(p, sec, auth_mysql_cleanup, apr_pool_cleanup_null);
#else
ap_register_cleanup(p, sec, auth_mysql_cleanup, ap_null_cleanup);
#endif
sec->dir = d;
sec->user_table = sec->group_table = NULL;
sec->user_field = sec->password_field = sec->group_field = NULL;
sec->group_where_clause = sec->password_where_clause = NULL;
sec->group_user_field = NULL;
sec->authoritative = 1;
sec->allow_empty_passwords = 1;
sec->dbh_error_lastchance = 0;
#ifdef DEBUG
syslog(LOG_DEBUG, "MAMDEBUG: Enabling MySQL auth by default");
#endif
sec->enable_mysql_auth = 1;
#ifdef CRYPT_DES
sec->encryption_types = CRYPT_DES_ENCRYPTION_FLAG;
sec->using_encryption_types = 0;
#else
sec->encryption_types = 0;
sec->using_encryption_types = 0;
#endif
sec->db_port = -1;
#ifdef DEBUG
syslog(LOG_DEBUG, "MAMDEBUG: Persistent is now ON");
#endif
sec->persistent = 1;
#ifdef DEBUG
for (i = 0; i < 15; i++)
{
sec->sacrificial_lamb[i] = i % 10 + '0';
}
#endif
return sec;
}
/* Helper function to make some decisions about whether to use crypted
* passwords in response to "AuthMySQL_Encrypted_Passwords on" in a config
* file.
* XXX DEPRECATED XXX
*/
static const char *set_crypted_password_flag(cmd_parms *cmd, void *sconf, int arg)
{
mysql_auth_config_rec *sec = (mysql_auth_config_rec *) sconf;
if (sec->using_encryption_types) {
/* This setting is ignored if we're using Encryption_Types */
return NULL;
}
#ifdef CRYPT_DES
if (arg) {
sec->encryption_types |= CRYPT_DES_ENCRYPTION_FLAG;
} else {
sec->encryption_types &= ~CRYPT_DES_ENCRYPTION_FLAG;
if (!sec->encryption_types) {
sec->encryption_types = PLAINTEXT_ENCRYPTION_FLAG;
}
}
#endif
return NULL;
}
/* Equivalent to set_crypted_password_flag above, except that this time we're
* talking about MySQL-style scrambled passwords instead.
* XXX DEPRECATED XXX
*/
static const char *set_scrambled_password_flag(cmd_parms *cmd, void *sconf, int arg)
{
mysql_auth_config_rec *sec = (mysql_auth_config_rec *) sconf;
if (sec->using_encryption_types) {
/* This setting is ignored if we're using Encryption_Types */
return NULL;
}
if (arg) {
sec->encryption_types |= MYSQL_ENCRYPTION_FLAG;
} else {
sec->encryption_types &= ~MYSQL_ENCRYPTION_FLAG;
if (!sec->encryption_types) {
sec->encryption_types = PLAINTEXT_ENCRYPTION_FLAG;
}
}
return NULL;
}
/* Ensure that any string passed through us won't unduly upset the MySQL
* server when passed in as part of a query.
*/
#ifdef APACHE2
static char *mysql_escape(char *str, apr_pool_t *p)
#else
static char *mysql_escape(char *str, pool *p)
#endif
{
char *dest;
if (!str) {
return NULL;
}
dest = (char *) PALLOC(p, strlen(str) * 2 + 1);
if (!dest) {
return str;
}
mysql_escape_string(dest, str, strlen(str));
return dest;
}
/* Config helper to set the server-wide default database name.
*/
static const char *set_auth_mysql_db(cmd_parms * parms, void *dummy, const char *db)
{
auth_db_name = (char *)db;
return NULL;
}
/* Config helper to set the server-wide default database host.
*/
static const char *set_auth_mysql_host(cmd_parms *parms, void *dummy, const char *host)
{
auth_db_host = (char *) host;
return NULL;
}
/* Config helper to set server-wide defaults for database parameters.
*/
static const char *set_auth_mysql_info(cmd_parms * parms, void *dummy, const char *host, const char *user, const char *pwd)
{
if (*host != '.') {
auth_db_host = (char *) host;
}
if (*user != '.') {
auth_db_user = (char *)user;
}
if (*pwd != '.') {
auth_db_pwd = (char *)pwd;
}
return NULL;
}
/* Config helper to set the server-wide default database username.
*/
static const char *set_auth_mysql_user(cmd_parms *parms, void *dummy, const char *user)
{
auth_db_user = (char *)user;
return NULL;
}
/* Config helper to set the server-wide default database password (coupled to
* the user specified above).
*/
static const char *set_auth_mysql_pwd(cmd_parms *parms, void *dummy, const char *pwd)
{
auth_db_pwd = (char *)pwd;
return NULL;
}
/* Set the server-wide database server socket.
*/
static const char *set_auth_mysql_socket(cmd_parms *parms, void *dummy, const char *sock)
{
auth_db_socket = (char *)socket;
return NULL;
}
/* Set the server-wide database server port.
*/
static const char *set_auth_mysql_port(cmd_parms *parms, void *dummy, const char *port)
{
auth_db_port = (unsigned int) atoi(port);
return NULL;
}
/* Config helper to judge whether to allow per-directory configs to override
* the server-wide defaults for database parameters. The only reason this
* exists (instead of using an ap_set_flag_slot) is because this isn't part
* of a config structure, and I'm not sure how to set globals from the Apache
* config thing.
*/
static const char *set_auth_mysql_override(cmd_parms *parms, void *dummy, int arg)
{
auth_db_override = arg;
return NULL;
}
/* Config helper to set a selected encryption type.
*/
static const char *set_encryption_types(cmd_parms *cmd, void *sconf, const char *arg)
{
mysql_auth_config_rec *sec = (mysql_auth_config_rec *) sconf;
int new_encryption_flag = get_encryption_flag(arg);
if (!new_encryption_flag) {
APACHELOG(APLOG_ERR, cmd, "Unsupported encryption type: %s", arg);
return NULL;
}
if (!sec->using_encryption_types) {
sec->encryption_types = 0;
sec->using_encryption_types = 1;
}
sec->encryption_types |= new_encryption_flag;
return NULL;
}
/* This pair of config helpers exist only because of varying semantics
* in the two versions of mod_auth_mysql I merged. As soon as we have a
* consistent set of configuration primitives, these are going.
*/
static const char *set_non_persistent(cmd_parms *cmd, void *sconf, int arg)
{
mysql_auth_config_rec *sec = (mysql_auth_config_rec *) sconf;
sec->persistent = !arg;
APACHELOG(APLOG_DEBUG, cmd, "set_non_persistent: Setting persistent in %s to %i", sec->dir, sec->persistent);
return NULL;
}
static const char *set_persistent(cmd_parms *cmd, void *sconf, int arg)
{
mysql_auth_config_rec *sec = (mysql_auth_config_rec *) sconf;
sec->persistent = arg;
APACHELOG(APLOG_DEBUG, cmd, "set_persistent: Setting persistent in %s to %i", sec->dir, sec->persistent);
return NULL;
}
static const char *enable_mysql(cmd_parms *cmd, void *sconf, int arg)
{
mysql_auth_config_rec *sec = (mysql_auth_config_rec *) sconf;
sec->enable_mysql_auth = arg;
APACHELOG(APLOG_DEBUG, cmd, "enable_mysql: Setting enable_mysql_auth in %s to %i", sec->dir, sec->enable_mysql_auth);
return NULL;
}
/* The command list. What it's called, when it's legal to use it, and
* what to do when we find it. Pretty cool, IMHO.
*/
#ifdef APACHE2
static
command_rec mysql_auth_cmds[] = {
AP_INIT_TAKE3( "Auth_MySQL_Info", set_auth_mysql_info,
NULL,
RSRC_CONF, "host, user and password of the MySQL database" ),
AP_INIT_TAKE1( "AuthMySQL_DefaultHost", set_auth_mysql_host,
NULL,
RSRC_CONF, "Default MySQL host" ),
AP_INIT_TAKE1( "AuthMySQL_DefaultUser", set_auth_mysql_user,
NULL,
RSRC_CONF, "Default MySQL user" ),
AP_INIT_TAKE1( "AuthMySQL_DefaultPassword", set_auth_mysql_pwd,
NULL,
RSRC_CONF, "Default MySQL password" ),
AP_INIT_TAKE1( "Auth_MySQL_DefaultPort", set_auth_mysql_port,
NULL,
RSRC_CONF, "Default MySQL server port" ),
AP_INIT_TAKE1( "Auth_MySQL_DefaultSocket", set_auth_mysql_socket,
NULL,
RSRC_CONF, "Default MySQL server socket" ),
AP_INIT_TAKE1( "Auth_MySQL_General_DB", set_auth_mysql_db,
NULL,
RSRC_CONF, "default database for MySQL authentication" ),
AP_INIT_TAKE1( "AuthMySQL_DefaultDB", set_auth_mysql_db,
NULL,
RSRC_CONF, "default database for MySQL authentication" ),
AP_INIT_TAKE1( "AuthMySQL_Host", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, db_host),
OR_AUTHCFG, "database host" ),
AP_INIT_TAKE1( "Auth_MySQL_Host", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, db_host),
OR_AUTHCFG, "database host" ),
AP_INIT_TAKE1( "Auth_MySQL_Socket", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, db_socket),
OR_AUTHCFG, "database host socket" ),
AP_INIT_TAKE1( "AuthMySQL_Socket", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, db_socket),
OR_AUTHCFG, "database host socket" ),
AP_INIT_TAKE1( "Auth_MySQL_Port", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, db_port),
OR_AUTHCFG, "database host port" ),
AP_INIT_TAKE1( "AuthMySQL_Port", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, db_port),
OR_AUTHCFG, "database host port" ),
AP_INIT_TAKE1( "Auth_MySQL_Username", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, db_user),
OR_AUTHCFG, "database user" ),
AP_INIT_TAKE1( "AuthMySQL_User", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, db_user),
OR_AUTHCFG, "database user" ),
AP_INIT_TAKE1( "Auth_MySQL_Password", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, db_pwd),
OR_AUTHCFG, "database password" ),
AP_INIT_TAKE1( "AuthMySQL_Password", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, db_pwd),
OR_AUTHCFG, "database password" ),
AP_INIT_TAKE1( "Auth_MySQL_DB", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, db_name),
OR_AUTHCFG, "database name" ),
AP_INIT_TAKE1( "AuthMySQL_DB", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, db_name),
OR_AUTHCFG, "database name" ),
AP_INIT_TAKE1( "Auth_MySQL_Password_Table", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, user_table),
OR_AUTHCFG, "Name of the MySQL table containing the password/user-name combination" ),
AP_INIT_TAKE1( "AuthMySQL_Password_Table", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, user_table),
OR_AUTHCFG, "Name of the MySQL table containing the password/user-name combination" ),
AP_INIT_TAKE1( "Auth_MySQL_Group_Table", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, group_table),
OR_AUTHCFG, "Name of the MySQL table containing the group-name/user-name combination; can be the same as the password-table." ),
AP_INIT_TAKE1( "Auth_MySQL_Group_Clause", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, group_where_clause),
OR_AUTHCFG, "Additional WHERE clause for group/user-name lookup" ),
AP_INIT_TAKE1( "AuthMySQL_Group_Table", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, group_table),
OR_AUTHCFG, "Name of the MySQL table containing the group-name/user-name combination; can be the same as the password-table." ),
AP_INIT_TAKE1( "Auth_MySQL_Password_Field", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, password_field),
OR_AUTHCFG, "The name of the field in the MySQL password table" ),
AP_INIT_TAKE1( "AuthMySQL_Password_Field", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, password_field),
OR_AUTHCFG, "The name of the field in the MySQL password table" ),
AP_INIT_TAKE1( "Auth_MySQL_Password_Clause", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, password_where_clause),
OR_AUTHCFG, "Additional WHERE clause for group password/user-name lookup" ),
AP_INIT_TAKE1( "Auth_MySQL_Username_Field", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, user_field),
OR_AUTHCFG, "The name of the user-name field in the MySQL password (and possibly group) table(s)." ),
AP_INIT_TAKE1( "AuthMySQL_Username_Field", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, user_field),
OR_AUTHCFG, "The name of the user-name field in the MySQL password (and possibly group) table(s)." ),
AP_INIT_TAKE1( "Auth_MySQL_Group_Field", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, group_field),
OR_AUTHCFG, "The name of the group field in the MySQL group table; must be set if you want to use groups." ),
AP_INIT_TAKE1( "AuthMySQL_Group_Field", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, group_field),
OR_AUTHCFG, "The name of the group field in the MySQL group table; must be set if you want to use groups." ),
AP_INIT_TAKE1( "Auth_MySQL_Group_User_Field", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, group_user_field),
OR_AUTHCFG, "The name of the user-name field in the MySQL group table; defaults to the same as the username field for the password table." ),
AP_INIT_TAKE1( "AuthMySQL_Group_User_Field", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, group_user_field),
OR_AUTHCFG, "The name of the user-name field in the MySQL group table; defaults to the same as the username field for the password table." ),
AP_INIT_FLAG( "Auth_MySQL_Empty_Passwords", ap_set_flag_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, allow_empty_passwords),
OR_AUTHCFG, "Enable (on) or disable (off) empty password strings; in which case any user password is accepted." ),
AP_INIT_FLAG( "AuthMySQL_Empty_Passwords", ap_set_flag_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, allow_empty_passwords),
OR_AUTHCFG, "Enable (on) or disable (off) empty password strings; in which case any user password is accepted." ),
AP_INIT_FLAG( "Auth_MySQL_Authoritative", ap_set_flag_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, authoritative),
OR_AUTHCFG, "When 'on' the MySQL database is taken to be authoritative and access control is not passed along to other db or access modules." ),
AP_INIT_FLAG( "AuthMySQL_Authoritative", ap_set_flag_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, authoritative),
OR_AUTHCFG, "When 'on' the MySQL database is taken to be authoritative and access control is not passed along to other db or access modules." ),
AP_INIT_FLAG( "AuthMySQL_AllowOverride", set_auth_mysql_override,
NULL,
RSRC_CONF, "Allow directory overrides of configuration" ),
AP_INIT_FLAG( "Auth_MySQL_Encrypted_Passwords", set_crypted_password_flag,
NULL,
OR_AUTHCFG, "When 'on' the password in the password table are taken to be crypt()ed using your machines crypt() function." ),
AP_INIT_FLAG( "AuthMySQL_Encrypted_Passwords", set_crypted_password_flag,
NULL,
OR_AUTHCFG, "When 'on' the password in the password table are taken to be crypt()ed using your machines crypt() function." ),
AP_INIT_FLAG( "Auth_MySQL_Scrambled_Passwords", set_scrambled_password_flag,
NULL,
OR_AUTHCFG, "When 'on' the password in the password table are taken to be scramble()d using mySQL's password() function." ),
AP_INIT_FLAG( "AuthMySQL_Scrambled_Passwords", set_scrambled_password_flag,
NULL,
OR_AUTHCFG, "When 'on' the password in the password table are taken to be scramble()d using mySQL's password() function." ),
AP_INIT_ITERATE( "Auth_MySQL_Encryption_Types", set_encryption_types,
NULL,
OR_AUTHCFG, "Encryption types to use" ),
AP_INIT_ITERATE( "AuthMySQL_Encryption_Types", set_encryption_types,
NULL,
OR_AUTHCFG, "Encryption types to use" ),
AP_INIT_FLAG( "Auth_MySQL_Non_Persistent", set_non_persistent,
NULL,
OR_AUTHCFG, "Use non-persistent MySQL links" ),
AP_INIT_FLAG( "AuthMySQL_Persistent", set_persistent,
NULL,
OR_AUTHCFG, "Use non-persistent MySQL links" ),
AP_INIT_FLAG( "Auth_MySQL", enable_mysql,
NULL,
OR_AUTHCFG, "Enable MySQL authentication" ),
AP_INIT_FLAG( "AuthMySQL", enable_mysql,
NULL,
OR_AUTHCFG, "Enable MySQL authentication" ),
AP_INIT_TAKE1( "Auth_MySQL_Where", ap_set_string_slot,
(void*)APR_XtOffsetOf(mysql_auth_config_rec, password_where_clause),
OR_AUTHCFG, "Additional WHERE clause for group password/user-name lookup" ),
{ NULL }
};
#else
command_rec mysql_auth_cmds[] = {
{ "Auth_MySQL_Info", set_auth_mysql_info,
NULL,
RSRC_CONF, TAKE3, "host, user and password of the MySQL database" },
{ "AuthMySQL_DefaultHost", set_auth_mysql_host,
NULL,
RSRC_CONF, TAKE1, "Default MySQL host" },
{ "AuthMySQL_DefaultUser", set_auth_mysql_user,
NULL,
RSRC_CONF, TAKE1, "Default MySQL user" },
{ "AuthMySQL_DefaultPassword", set_auth_mysql_pwd,
NULL,
RSRC_CONF, TAKE1, "Default MySQL password" },
{ "Auth_MySQL_DefaultPort", set_auth_mysql_port,
NULL,
RSRC_CONF, TAKE1, "Default MySQL server port" },
{ "Auth_MySQL_DefaultSocket", set_auth_mysql_socket,
NULL,
RSRC_CONF, TAKE1, "Default MySQL server socket" },
{ "Auth_MySQL_General_DB", set_auth_mysql_db,
NULL,
RSRC_CONF, TAKE1, "default database for MySQL authentication" },
{ "AuthMySQL_DefaultDB", set_auth_mysql_db,
NULL,
RSRC_CONF, TAKE1, "default database for MySQL authentication" },
{ "AuthMySQL_Host", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, db_host),
OR_AUTHCFG, TAKE1, "database host" },
{ "Auth_MySQL_Host", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, db_host),
OR_AUTHCFG, TAKE1, "database host" },
{ "Auth_MySQL_Socket", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, db_socket),
OR_AUTHCFG, TAKE1, "database host socket" },
{ "Auth_MySQL_Port", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, db_port),
OR_AUTHCFG, TAKE1, "database host socket" },
{ "Auth_MySQL_Username", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, db_user),
OR_AUTHCFG, TAKE1, "database user" },
{ "AuthMySQL_User", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, db_user),
OR_AUTHCFG, TAKE1, "database user" },
{ "Auth_MySQL_Password", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, db_pwd),
OR_AUTHCFG, TAKE1, "database password" },
{ "AuthMySQL_Password", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, db_pwd),
OR_AUTHCFG, TAKE1, "database password" },
{ "Auth_MySQL_DB", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, db_name),
OR_AUTHCFG, TAKE1, "database name" },
{ "AuthMySQL_DB", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, db_name),
OR_AUTHCFG, TAKE1, "database name" },
{ "Auth_MySQL_Password_Table", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, user_table),
OR_AUTHCFG, TAKE1, "Name of the MySQL table containing the password/user-name combination" },
{ "AuthMySQL_Password_Table", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, user_table),
OR_AUTHCFG, TAKE1, "Name of the MySQL table containing the password/user-name combination" },
{ "Auth_MySQL_Group_Table", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, group_table),
OR_AUTHCFG, TAKE1, "Name of the MySQL table containing the group-name/user-name combination; can be the same as the password-table." },
{ "Auth_MySQL_Group_Clause", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, group_where_clause),
OR_AUTHCFG, TAKE1, "Additional WHERE clause for group/user-name lookup" },
{ "AuthMySQL_Group_Table", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, group_table),
OR_AUTHCFG, TAKE1, "Name of the MySQL table containing the group-name/user-name combination; can be the same as the password-table." },
{ "Auth_MySQL_Password_Field", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, password_field),
OR_AUTHCFG, TAKE1, "The name of the field in the MySQL password table" },
{ "AuthMySQL_Password_Field", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, password_field),
OR_AUTHCFG, TAKE1, "The name of the field in the MySQL password table" },
{ "Auth_MySQL_Password_Clause", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, password_where_clause),
OR_AUTHCFG, TAKE1, "Additional WHERE clause for group password/user-name lookup" },
{ "Auth_MySQL_Username_Field", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, user_field),
OR_AUTHCFG, TAKE1, "The name of the user-name field in the MySQL password (and possibly group) table(s)." },
{ "AuthMySQL_Username_Field", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, user_field),
OR_AUTHCFG, TAKE1, "The name of the user-name field in the MySQL password (and possibly group) table(s)." },
{ "Auth_MySQL_Group_Field", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, group_field),
OR_AUTHCFG, TAKE1, "The name of the group field in the MySQL group table; must be set if you want to use groups." },
{ "AuthMySQL_Group_Field", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, group_field),
OR_AUTHCFG, TAKE1, "The name of the group field in the MySQL group table; must be set if you want to use groups." },
{ "Auth_MySQL_Group_User_Field", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, group_user_field),
OR_AUTHCFG, TAKE1, "The name of the user-name field in the MySQL group table; defaults to the same as the username field for the password table." },
{ "AuthMySQL_Group_User_Field", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, group_user_field),
OR_AUTHCFG, TAKE1, "The name of the user-name field in the MySQL group table; defaults to the same as the username field for the password table." },
{ "Auth_MySQL_Empty_Passwords", ap_set_flag_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, allow_empty_passwords),
OR_AUTHCFG, FLAG, "Enable (on) or disable (off) empty password strings; in which case any user password is accepted." },
{ "AuthMySQL_Empty_Passwords", ap_set_flag_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, allow_empty_passwords),
OR_AUTHCFG, FLAG, "Enable (on) or disable (off) empty password strings; in which case any user password is accepted." },
{ "Auth_MySQL_Authoritative", ap_set_flag_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, authoritative),
OR_AUTHCFG, FLAG, "When 'on' the MySQL database is taken to be authoritative and access control is not passed along to other db or access modules." },
{ "AuthMySQL_Authoritative", ap_set_flag_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, authoritative),
OR_AUTHCFG, FLAG, "When 'on' the MySQL database is taken to be authoritative and access control is not passed along to other db or access modules." },
{ "AuthMySQL_AllowOverride", set_auth_mysql_override,
NULL,
RSRC_CONF, FLAG, "Allow directory overrides of configuration" },
{ "Auth_MySQL_Encrypted_Passwords", set_crypted_password_flag,
NULL,
OR_AUTHCFG, FLAG, "When 'on' the password in the password table are taken to be crypt()ed using your machines crypt() function." },
{ "AuthMySQL_Encrypted_Passwords", set_crypted_password_flag,
NULL,
OR_AUTHCFG, FLAG, "When 'on' the password in the password table are taken to be crypt()ed using your machines crypt() function." },
{ "Auth_MySQL_Scrambled_Passwords", set_scrambled_password_flag,
NULL,
OR_AUTHCFG, FLAG, "When 'on' the password in the password table are taken to be scramble()d using mySQL's password() function." },
{ "AuthMySQL_Scrambled_Passwords", set_scrambled_password_flag,
NULL,
OR_AUTHCFG, FLAG, "When 'on' the password in the password table are taken to be scramble()d using mySQL's password() function." },
{ "Auth_MySQL_Encryption_Types", set_encryption_types,
NULL,
OR_AUTHCFG, ITERATE,"Encryption types to use" },
{ "AuthMySQL_Encryption_Types", set_encryption_types,
NULL,
OR_AUTHCFG, ITERATE,"Encryption types to use" },
{ "Auth_MySQL_Non_Persistent", set_non_persistent,
NULL,
OR_AUTHCFG, FLAG, "Use non-persistent MySQL links" },
{ "AuthMySQL_Persistent", set_persistent,
NULL,
OR_AUTHCFG, FLAG, "Use non-persistent MySQL links" },
{ "Auth_MySQL", enable_mysql,
NULL,
OR_AUTHCFG, FLAG, "Enable MySQL authentication" },
{ "AuthMySQL", enable_mysql,
NULL,
OR_AUTHCFG, FLAG, "Enable MySQL authentication" },
{ "Auth_MySQL_Where", ap_set_string_slot,
(void *) XtOffsetOf(mysql_auth_config_rec, password_where_clause),
OR_AUTHCFG, TAKE1, "Additional WHERE clause for group password/user-name lookup" },
{ NULL }
};
/* { "Auth_MySQL", ap_set_flag_slot, (void *) XtOffsetOf(mysql_auth_config_rec, enable_mysql_auth), OR_AUTHCFG, FLAG, "Enable (on) or disable (off) MySQL authentication." },
{ "AuthMySQL", ap_set_flag_slot, (void *) XtOffsetOf(mysql_auth_config_rec, enable_mysql_auth), OR_AUTHCFG, FLAG, "Enable (on) or disable (off) MySQL authentication." },
*/
#endif
#ifdef APACHE2
static apr_status_t
#else
static void
#endif
auth_mysql_result_cleanup(void *result)
{
mysql_free_result((MYSQL_RES *) result);
}
#ifdef APACHE2
static void note_cleanups_for_mysql_auth_result(apr_pool_t *p, MYSQL_RES * result)
#else
static void note_cleanups_for_mysql_auth_result(pool *p, MYSQL_RES * result)
#endif
{
#ifdef APACHE2
apr_pool_cleanup_register(p, (void *) result, auth_mysql_result_cleanup, auth_mysql_result_cleanup);
#else
ap_register_cleanup(p, (void *) result, auth_mysql_result_cleanup, auth_mysql_result_cleanup);
#endif
}
/* Make a MySQL database link open and ready for business. Returns 0 on
* success, or the MySQL error number which caused the failure if there was
* some sort of problem.
*/
static int open_auth_dblink(request_rec *r, mysql_auth_config_rec *sec)
{
char *host = "localhost", *socket = NULL;
unsigned int port = 3306;
char *dbname = auth_db_name, *user = auth_db_user, *pwd = auth_db_pwd;
void (*sigpipe_handler)();
unsigned long client_flag = 0;
APACHELOG(APLOG_DEBUG, r, "Opening DB connection for %s", sec->dir);
if (auth_db_host) {
host = auth_db_host;
}
if (auth_db_socket)
{
socket = auth_db_socket;
}
if (auth_db_port != -1)
{
port = auth_db_port;
}
if (auth_db_override)
{
if (sec->db_socket)
{
socket = sec->db_socket;
}
if (sec->db_port != -1)
{
port = sec->db_port;
}
if (sec->db_host)
{
host = sec->db_host;
}
if (sec->db_user) {
user = sec->db_user;
}
if (sec->db_pwd) {
pwd = sec->db_pwd;
}
if (sec->db_name) {
dbname = sec->db_name;
}
}
if (!dbname || !dbname[0]) {
/* It would be preferred if we had somewhere to connect to... */
APACHELOG(APLOG_CRIT, r,
"No database given - rather a problem. Bailing out.");
return CR_WRONG_HOST_INFO;
}
/* MySQL likes to throw the odd SIGPIPE now and then - ignore it for now */
sigpipe_handler = signal(SIGPIPE, SIG_IGN);
sec->dbh = mysql_init(NULL);
if (!mysql_real_connect(sec->dbh, host, user, pwd, dbname, port, socket, client_flag)) {
APACHELOG(APLOG_ERR, r,
"Connection error: %s", mysql_error(sec->dbh));
errno = mysql_errno(sec->dbh);
mysql_close(sec->dbh);
sec->dbh = NULL;
return errno;
}
signal(SIGPIPE, sigpipe_handler);
APACHELOG(APLOG_DEBUG, r, "Persistent in %s is %i", sec->dir, sec->persistent);
if (!sec->persistent) {
APACHELOG(APLOG_DEBUG, r, "Registering non-persistent for %s", sec->dir);
#ifdef APACHE2
apr_pool_cleanup_register(r->pool, sec, auth_mysql_cleanup, apr_pool_cleanup_null);
#else
ap_block_alarms();
ap_register_cleanup(r->pool, sec, auth_mysql_cleanup, ap_null_cleanup);
ap_unblock_alarms();
#endif
}
/* W00t! We made it! */
return 0;
}
/* Run a query against the database. Doesn't assume nearly anything about
* the state of affairs regarding the database connection.
* Returns 0 on a successful query run, or the MySQL error number on
* error. It is the responsibility of the calling function to retrieve any
* data which may have been obtained through the running of this function.
*/
static int safe_mysql_query(request_rec *r, char *query, mysql_auth_config_rec *sec)
{
int error = CR_UNKNOWN_ERROR;
APACHELOG(APLOG_DEBUG, r, "sec->dbh in %s is %p", sec->dir, sec->dbh);
if (sec->dbh_error_lastchance)
{
APACHELOG(APLOG_DEBUG, r, "Last chance, bub");
}
else
{
APACHELOG(APLOG_DEBUG, r, "Ordinary query");
}
if (!sec->dbh) {
APACHELOG(APLOG_DEBUG, r,
"No DB connection open - firing one up");
if ((error = open_auth_dblink(r, sec))) {
APACHELOG(APLOG_DEBUG, r,
"open_auth_dblink returned %i", error);
return error;
}
APACHELOG(APLOG_DEBUG, r,
"Correctly opened a new DB connection");
}
APACHELOG(APLOG_DEBUG, r,
"Running query: [%s]", query);
if (mysql_query(sec->dbh, query)) {
error = mysql_errno(sec->dbh);
APACHELOG(APLOG_DEBUG, r,
"Query maybe-failed: %s (%i), lastchance=%i", mysql_error(sec->dbh), error, sec->dbh_error_lastchance);
APACHELOG(APLOG_DEBUG, r,
"Error numbers of interest are %i (SG) and %i (SL)",
CR_SERVER_GONE_ERROR, CR_SERVER_LOST);
if (sec->dbh_error_lastchance)
{
/* No matter what error, we're moving out */
return error;
}
else if (error == CR_SERVER_LOST || error == CR_SERVER_GONE_ERROR)
{
/* Try again, once more only */
sec->dbh_error_lastchance = 1;
sec->dbh = NULL;
APACHELOG(APLOG_DEBUG, r, "Retrying query");
return safe_mysql_query(r, query, sec);
}
else
{
return error;
}
}
return 0;
}
/* Store the result of a query in a result structure, and return it. It's
* "safe" in the fact that a cleanup function is registered for the structure
* so it will be tidied up after the request.
* Returns the result data on success, or NULL if there was no data to retrieve.
*/
#ifdef APACHE2
static MYSQL_RES *safe_mysql_store_result(apr_pool_t *p, mysql_auth_config_rec *sec)
#else
static MYSQL_RES *safe_mysql_store_result(pool *p, mysql_auth_config_rec *sec)
#endif
{
MYSQL_RES *result;
#ifdef APACHE2
#else
ap_block_alarms();
#endif
result = mysql_store_result(sec->dbh);
#ifdef DEBUG
syslog(LOG_DEBUG, "MAMDEBUG: Got %p for result", result);
#endif
if (result) {
note_cleanups_for_mysql_auth_result(p, result);
}
#ifdef APACHE2
#else
ap_unblock_alarms();
#endif
return result;
}
/* Check the plaintext password given against the hashed version. Go
* through all configured encryption types looking for a match.
* Returns 1 on a match, 0 on no match, and -1 on error.
*/
static int check_password(const char *plaintext, char *hashed, request_rec *r, mysql_auth_config_rec *sec)
{
encryption_type_entry *ete;
/* empty password support */
if (sec->allow_empty_passwords && !strlen(hashed)) {
APACHELOG(APLOG_INFO, r, "User successful on empty password");
return 1;
}
for (ete=supported_encryption_types; ete->name; ete++) {
if (sec->encryption_types & ete->flag) {
APACHELOG(APLOG_DEBUG, r,
"Checking with %s", ete->name);
if (ete->check_function(plaintext, hashed)) {
APACHELOG(APLOG_DEBUG, r, "Auth succeeded");
return 1;
}
}
}
APACHELOG(APLOG_DEBUG, r, "User failed all encryption types");
return 0;
}
/* Checks whether the username and plaintext password match the user data
* stored in the database, against all configured encryption schemes.
* Returns 1 on successful match, 0 unsuccessful match, -1 on error.
*/
static int mysql_check_user_password(request_rec *r, char *user, const char *password, mysql_auth_config_rec *sec)
{
char *auth_table = "mysql_auth", *auth_user_field = "username",
*auth_password_field = "passwd", *auth_password_clause = "";
char *query;
char *esc_user = mysql_escape(user, r->pool);
MYSQL_RES *result;
MYSQL_ROW sql_row;
int rv;
if (sec->user_table) {
auth_table = sec->user_table;
}
if (sec->user_field) {
auth_user_field = sec->user_field;
}
if (sec->password_field) {
auth_password_field = sec->password_field;
}
if (sec->password_where_clause) {
auth_password_clause = sec->password_where_clause;
}
APACHELOG(APLOG_DEBUG, r,
"Constructing password collection query with "
"passfield=[%s], table=[%s], userfield=[%s], where_clause=[%s]", auth_password_field
, auth_table, esc_user,auth_password_clause);
query = (char *) PSTRCAT(r->pool, "SELECT ", auth_password_field,
" FROM ", auth_table, " WHERE ",
auth_user_field, "='", esc_user, "'",
auth_password_clause, NULL);
if (!query) {
APACHELOG(APLOG_ERR, r,
"Failed to create query string - we're in deep poopy");
return -1;
}
if ((rv = safe_mysql_query(r, query, sec))) {
if (sec->dbh)
{
APACHELOG(APLOG_ERR, r,
"Query call failed: %s (%i)", mysql_error(sec->dbh), rv);
}
APACHELOG(APLOG_DEBUG, r, "Failed query was: [%s]", query);
return -1;
}
result = safe_mysql_store_result(r->pool, sec);
if (!result) {
APACHELOG(APLOG_ERR, r,
"Failed to get MySQL result structure : %s", mysql_error(sec->dbh));
return -1;
}
switch (mysql_num_rows(result)) {
case 0:
APACHELOG(APLOG_INFO, r, "User not found");
return 0;
break;
case 1:
sql_row = mysql_fetch_row(result);
/* ensure we have a row, and non NULL value */
if (!sql_row || !sql_row[0]) {
APACHELOG(APLOG_INFO, r,
"No row returned or NULL value: %s", mysql_error(sec->dbh));
return -1;
}
rv = check_password(password, sql_row[0], r, sec);
if (rv == 0)
{
APACHELOG(APLOG_INFO, r,
"Authentication failed for user %s", user);
}
return rv;
break;
default:
APACHELOG(APLOG_ERR, r,
"Multiple password rows returned - this is what is known, in the industry, as a Bad Thing");
return -1;
break;
}
APACHELOG(APLOG_CRIT, r, "Can't happen - dropped out of switch!");
return -1;
}
/* Has a look to see if the given user is a member of the named group.
* Returns 0 if user is not a part of the group, 1 if he is, -1 on error.
*/
static int mysql_check_group(request_rec *r, char *user, char *group, mysql_auth_config_rec *sec)
{
char *auth_table = "mysql_auth", *auth_group_field="groups", *auth_group_clause="";
char *query;
char *esc_user = mysql_escape(user, r->pool);
char *esc_group = mysql_escape(group, r->pool);
MYSQL_RES *result;
MYSQL_ROW row;
char *auth_user_field = "username";
if (!group) {
APACHELOG(APLOG_ERR, r, "No group specified");
return 0;
}
if (sec->group_table) {
auth_table = sec->group_table;
}
if (sec->user_field)
{
auth_user_field = sec->user_field;
}
if (sec->group_user_field) {
auth_user_field = sec->group_user_field;
}
if (sec->group_field) {
auth_group_field = sec->group_field;
}
if (sec->group_where_clause) {
auth_group_clause = sec->group_where_clause;
}
APACHELOG(APLOG_DEBUG, r,
"Making group query with auth_table=[%s], auth_user_field=[%s], "
"esc_user=[%s], esc_group=[%s], auth_group_field=[%s], where_clause=[%s]",
auth_table, auth_user_field, esc_user, esc_group, auth_group_field,auth_group_clause);
query = (char *) PSTRCAT(r->pool, "SELECT count(*) FROM ", auth_table,
" WHERE ", auth_user_field, "='", esc_user, "'",
" and FIND_IN_SET('", esc_group, "',", auth_group_field, ")",
auth_group_clause, NULL);
APACHELOG(APLOG_DEBUG, r, "Group query created; [%s]", query);
if (!query) {
APACHELOG(APLOG_CRIT, r,
"Failed to create group-check query - ran out of memory!");
return -1;
}
if (safe_mysql_query(r, query, sec)) {
APACHELOG(APLOG_CRIT, r, "Group query failed!");
return -1;
}
result = safe_mysql_store_result(r->pool, sec);
if (!result || (row=mysql_fetch_row(result))==NULL || !row[0]) {
APACHELOG(APLOG_CRIT, r, "Store result failed - erp!");
return -1;
}
return atoi(row[0]);
}
/* The apache-called function. Note that this function says nothing about
* what the user should be allowed to do - merely that they have proved they
* are who they say they are. Return OK if the user has proved their
* identity, DECLINED if we are not taking any responsibility for them, or
* some Apache error if there was a problem.
*/
int mysql_authenticate_basic_user(request_rec *r)
{
mysql_auth_config_rec *sec = (mysql_auth_config_rec *) ap_get_module_config(r->per_dir_config, &auth_mysql_module);
conn_rec *c = r->connection;
const char *sent_pw;
int res;
APACHELOG(APLOG_DEBUG, r, "Handling an authentication request for section %s", sec->dir);
#ifdef DEBUG
for (res = 0; res < 512; res++)
{
if (sec->sacrificial_lamb[res] == '\0')
{
sec->sacrificial_lamb[res] = 'n';
}
if (!isgraph(sec->sacrificial_lamb[res]))
{
sec->sacrificial_lamb[res] = ' ';
}
}
sec->sacrificial_lamb[511] = '\0';
syslog(LOG_DEBUG, "The contents of the lamb are %s", sec->sacrificial_lamb);
#endif
if (!sec->enable_mysql_auth) {
APACHELOG(APLOG_DEBUG, r,
"Not running mod-auth-mysql for %s - disabled", r->unparsed_uri);
return DECLINED;
}
/* use MySQL auth only if we have a database */
if (!auth_db_name && !sec->db_name) {
APACHELOG(APLOG_ERR, r,
"Failed to run mod-auth-mysql for %s: No database name specified", r->unparsed_uri);
return DECLINED;
}
/* obtain sent password */
if ((res = ap_get_basic_auth_pw(r, &sent_pw))) {
return res;
}
#ifdef APACHE2
APACHELOG(APLOG_DEBUG, r,
"Starting basic user auth for [%s] in %s, child pid %i",
r->user,
sec->dir, getpid());
#else
APACHELOG(APLOG_DEBUG, r,
"Starting basic user auth for [%s] in %s, child pid %i",
c->user,
sec->dir, getpid());
#endif
#ifdef APACHE2
switch (mysql_check_user_password(r, r->user, sent_pw, sec)) {
#else
switch (mysql_check_user_password(r, c->user, sent_pw, sec)) {
#endif
case 0:
ap_note_basic_auth_failure(r);
return HTTP_UNAUTHORIZED;
break;
case 1:
return OK;
break;
case -1:
default:
APACHELOG(APLOG_DEBUG, r,
"mysql_check_user_password returned error");
return HTTP_INTERNAL_SERVER_ERROR;
break;
}
}
/* Go through a 'requires' line configured for the module, and return OK
* if the user satisfies the line, or some sort of failure return code
* otherwise.
*/
int check_mysql_auth_require(char *user, const char *t, request_rec *r)
{
mysql_auth_config_rec *sec = (mysql_auth_config_rec *) ap_get_module_config(r->per_dir_config, &auth_mysql_module);
const char *w;
int rv;
w = ap_getword(r->pool, &t, ' ');
/* If they're letting any old authenticated user, we're off the
* hook!
*/
if (!strcmp(w, "valid-user")) {
return OK;
}
/* Checking a list of usernames */
if (!strcmp(w, "user")) {
while (t[0]) {
w = ap_getword_conf(r->pool, &t);
if (!strcmp(user, w)) {
return OK;
}
}
/* Not found */
return HTTP_UNAUTHORIZED;
} else if (!strcmp(w, "group")) {
/* This is the prickly one; checking whether the
* user is a member of a listed group.
*/
while (t[0])
{
w = ap_getword_conf(r->pool, &t);
rv = mysql_check_group(r, user, (char *)w, sec);
if (rv == 1)
{
/* Yep, we're all good */
return OK;
}
else if (rv == -1)
{
return HTTP_INTERNAL_SERVER_ERROR;
}
}
/* Distinct lack of foundage */
return HTTP_UNAUTHORIZED;
}
else
{
APACHELOG(APLOG_ERR, r, "Invalid argument to require: %s", w);
return HTTP_INTERNAL_SERVER_ERROR;
}
APACHELOG(APLOG_ERR, r, "CAN'T HAPPEN: Dropped out of the bottom of check_mysql_auth_require!");
return HTTP_INTERNAL_SERVER_ERROR;
}
/* This is the authorization step. We're presuming that the user has
* successfully negotiated the step of "I am who I say I am", now we're
* checking to see if the user has permission to access this particular
* resource. As with mysql_authenticate_basic_user, above, we return OK if
* the user is fit to proceed, DECLINED if we don't want to make a decision
* either way, HTTP_UNAUTHORIZED if the user is not allowed, or some apache
* error if there was a major problem.
*/
int mysql_check_auth(request_rec *r)
{
mysql_auth_config_rec *sec = (mysql_auth_config_rec *) ap_get_module_config(r->per_dir_config, &auth_mysql_module);
#ifdef APACHE2
char *user = r->user;
#else
char *user = r->connection->user;
#endif
int m = r->method_number;
int rv;
register int x;
const char *t;
#ifdef APACHE2
const apr_array_header_t *reqs_arr = ap_requires(r);
#else
const array_header *reqs_arr = ap_requires(r);
#endif
require_line *reqs;
/* use MySQL auth only if we have a database */
if (!auth_db_name && !sec->db_name) {
return DECLINED;
}
/* What do we do if there's no requires line available? Either say
* "bad puppy" if we're king shit, or say "not my problem" otherwise.
*/
if (!reqs_arr) {
if (sec->authoritative) {
APACHELOG(APLOG_ERR, r, "No requires line available");
return HTTP_UNAUTHORIZED;
} else {
return DECLINED;
}
}
/* This is an array of all the requires lines which apply to us.
* There may be several, as in the case of something like:
* require user foo bar
* require group wombat
* That is, the user either has to belong to the group 'wombat' or
* be 'foo' or 'bar'.
* We have to check them all. Yuck.
*/
reqs = (require_line *) reqs_arr->elts;
for (x = 0; x < reqs_arr->nelts; x++) {
/* mjp: WTF is this? */
if (!(reqs[x].method_mask & (1 << m))) {
continue;
}
t = reqs[x].requirement;
/* OK, this might seem a little weird. The logic is that,
* if the user is approved, that's sufficient, so we can
* return OK straight away. Alternately, if there's an
* error, we bomb the check and die. The only circumstance
* where we continue looping is when the user didn't pass this
* check, but might pass a future one, so keep looking.
*/
if ((rv = check_mysql_auth_require(user, t, r))
!= HTTP_UNAUTHORIZED)
{
return rv;
}
}
/* We don't know, and we don't really care */
if (!(sec->authoritative)) {
return DECLINED;
}
ap_note_basic_auth_failure(r);
return HTTP_UNAUTHORIZED;
}
#ifdef APACHE2
static void register_hooks(apr_pool_t *p)
{
ap_hook_check_user_id(mysql_authenticate_basic_user, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_auth_checker(mysql_check_auth, NULL, NULL, APR_HOOK_MIDDLE);
}
#endif
#ifdef APACHE2
module AP_MODULE_DECLARE_DATA auth_mysql_module =
{
STANDARD20_MODULE_STUFF,
create_mysql_auth_dir_config, /* dir config creater */
NULL, /* dir merger --- default is to override */
NULL, /* server config */
NULL, /* merge server config */
mysql_auth_cmds, /* command apr_table_t */
register_hooks /* register hooks */
};
#else
module auth_mysql_module =
{
STANDARD_MODULE_STUFF,
mysql_auth_init_handler, /* initializer */
create_mysql_auth_dir_config, /* dir config creater */
NULL, /* dir merger --- default is to override */
NULL, /* server config */
NULL, /* merge server config */
mysql_auth_cmds, /* command table */
NULL, /* handlers */
NULL, /* filename translation */
mysql_authenticate_basic_user, /* check_user_id */
mysql_check_auth, /* check auth */
NULL, /* check access */
NULL, /* type_checker */
NULL, /* pre-run fixups */
NULL /* logger */
#if MODULE_MAGIC_NUMBER >= 19970103
,NULL /* header parser */
#endif
#if MODULE_MAGIC_NUMBER >= 19970719
,NULL /* child_init */
#endif
#if MODULE_MAGIC_NUMBER >= 19970728
,NULL /* child_exit */
#endif
#if MODULE_MAGIC_NUMBER >= 19970902
,NULL /* post read-request */
#endif
};
#endif
|