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 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
|
/*
* Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
* 2008, 2009, 2010, 2011, 2012, 2013, 2014
* Inferno Nettverk A/S, Norway. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. The above copyright notice, this list of conditions and the following
* disclaimer must appear in all copies of the software, derivative works
* or modified versions, and any portions thereof, aswell as in all
* supporting documentation.
* 2. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by
* Inferno Nettverk A/S, Norway.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Inferno Nettverk A/S requests users of this software to return to
*
* Software Distribution Coordinator or sdc@inet.no
* Inferno Nettverk A/S
* Oslo Research Park
* Gaustadallen 21
* NO-0349 Oslo
* Norway
*
* any improvements or extensions that they make and grant Inferno Nettverk A/S
* the rights to redistribute these changes.
*
*/
#include "common.h"
static const char rcsid[] =
"$Id: serverconfig.c,v 1.567.4.12 2014/08/24 11:41:34 karls Exp $";
static int
safamily_isenabled(const sa_family_t family, const char *addrstr,
const interfaceside_t side);
/*
* Returns true if the address family "family" is enabled on the
* interface-side "side". "addrstr" is a printable representation of
* the address we tried to add.
*
* Returns false if the address family "family" is not enabled.
*/
static int addexternaladdr(const struct ruleaddr_t *ra);
/*
* Returns 0 if the address "ra" was addedd to the list of external addresses.
*
* Returns -1 if the address "ra" was not added for a non-fatal reason,
* after logging a message if appropriate.
*/
static int addinternaladdr(const char *ifname,
const struct sockaddr_storage *sa,
const int protocol);
/*
* Returns 0 if the address "ra" was addedd to the list of internal addresses.
*
* Returns -1 if the address "ra" was not added for a non-fatal reason,
* after logging a message if appropriate.
*/
static void
add_more_old_shmem(struct config *config, const size_t memc,
const oldshmeminfo_t memv[]);
/*
* Adds "memv" to the list of old shmem entries stored in "config".
*/
struct config sockscf; /* current config. */
void
addinternal(addr, protocol)
const ruleaddr_t *addr;
const int protocol;
{
const char *function = "addinternal()";
struct sockaddr_storage sa;
char ifname[MAXIFNAMELEN];
int changesupported;
if (sockscf.option.serverc == 1
|| sockscf.state.inited == 0
|| protocol == SOCKS_UDP)
changesupported = 1;
else
changesupported = 0;
slog(LOG_DEBUG, "%s: (%s, %s). Change supported: %d",
function,
ruleaddr2string(addr,
ADDRINFO_PORT | ADDRINFO_ATYPE,
NULL,
0),
protocol2string(protocol),
changesupported);
switch (addr->atype) {
case SOCKS_ADDR_IPV4:
case SOCKS_ADDR_IPV6:
if (addr->atype == SOCKS_ADDR_IPV4)
SASSERTX(addr->addr.ipv4.mask.s_addr == htonl(IPV4_FULLNETMASK));
else if (addr->atype == SOCKS_ADDR_IPV6)
SASSERTX(addr->addr.ipv6.maskbits == IPV6_NETMASKBITS);
ruleaddr2sockaddr(addr, &sa, protocol);
if (!PORTISBOUND(&sa))
yyerrorx("%s: address %s does not specify a portnumber to bind",
function, sockaddr2string(&sa, NULL, 0));
if (addrindex_on_listenlist(sockscf.internal.addrc,
sockscf.internal.addrv,
&sa,
protocol) == -1) {
if (!changesupported) {
yywarnx("cannot change internal addresses once running. "
"%s looks like a new address and will be ignored",
sockaddr2string(&sa, NULL, 0));
break;
}
}
else {
/*
* Already here, but do make sure to update globalstate to reflect
* it too.
*/
add_internal_safamily(sa.ss_family);
break;
}
if (sa.ss_family == AF_INET
&& TOIN(&sa)->sin_addr.s_addr == htonl(INADDR_ANY))
STRCPY_ASSERTSIZE(ifname, "<any IPv4-interface>");
else if (sa.ss_family == AF_INET6
&& memcmp(&TOIN6(&sa)->sin6_addr,
&in6addr_any,
sizeof(in6addr_any)) == 0)
STRCPY_ASSERTSIZE(ifname, "<any IPv6-interface>");
else if (sockaddr2ifname(&sa, ifname, sizeof(ifname)) == NULL) {
/*
* Probably config-error, but could be a bug in sockaddr2ifname(),
* so don't error out yet. Will know for sure when we try to bind
* the address later.
*/
strncpy(ifname, "<unknown>", sizeof(ifname) - 1);
ifname[sizeof(ifname) - 1] = NUL;
yywarn("%s: could not find address %s on any network interface",
function, sockaddr2string2(&sa, 0, NULL, 0));
}
addinternaladdr(ifname, &sa, protocol);
break;
case SOCKS_ADDR_DOMAIN: {
size_t i;
char emsg[1024];
int gaierr;
for (i = 0;
hostname2sockaddr2(addr->addr.domain,
i,
&sa,
&gaierr,
emsg,
sizeof(emsg)) != NULL;
++i) {
SET_SOCKADDRPORT(&sa,
protocol == SOCKS_TCP ?
addr->port.tcp : addr->port.udp);
if (addrindex_on_listenlist(sockscf.internal.addrc,
sockscf.internal.addrv,
&sa,
protocol) == -1) {
if (!changesupported) {
swarnx("cannot change internal addresses once running "
"and %s looks like a new address. Ignored",
sockaddr2string(&sa, NULL, 0));
continue;
}
}
else {
/*
* Already here, but do make sure to update globalstate to
* reflect it too.
*/
add_internal_safamily(sa.ss_family);
continue;
}
if (sockaddr2ifname(&sa, ifname, sizeof(ifname)) == NULL) {
/*
* Probably config-error, but could be bug in our
* sockaddr2ifname().
* Will know for sure when we try to bind the address later,
* so don't error out quite yet.
*/
yywarn("%s: could not find address %s (resolved from %s) on "
"any network interface",
function,
sockaddr2string(&sa, NULL, 0),
addr->addr.domain);
STRCPY_ASSERTSIZE(ifname, "<unknown>");
}
addinternaladdr(ifname, &sa, protocol);
}
if (i == 0)
yyerrorx("%s", emsg);
break;
}
case SOCKS_ADDR_IFNAME: {
struct ifaddrs *ifap, *iface;
int isvalidif;
ifap = NULL;
if (getifaddrs(&ifap) != 0)
serr("getifaddrs()");
SASSERTX(ifap != NULL);
for (isvalidif = 0, iface = ifap;
iface != NULL;
iface = iface->ifa_next) {
if (iface->ifa_addr == NULL)
continue;
if (!safamily_issupported(iface->ifa_addr->sa_family))
continue;
if (strcmp(iface->ifa_name, addr->addr.ifname) != 0)
continue;
isvalidif = 1;
sockaddrcpy(&sa, TOSS(iface->ifa_addr), sizeof(sa));
SET_SOCKADDRPORT(&sa, protocol == SOCKS_TCP ?
addr->port.tcp : addr->port.udp);
if (addrindex_on_listenlist(sockscf.internal.addrc,
sockscf.internal.addrv,
&sa,
protocol) == -1) {
if (!changesupported) {
swarnx("cannot change internal addresses once running, "
"and %s, expanded from the ifname \"%s\" looks "
"like a new address. Ignored",
sockaddr2string(&sa, NULL, 0),
addr->addr.ifname);
continue;
}
}
else {
/*
* Already here, but do make sure to update globalstate to
* reflect it too.
*/
add_internal_safamily(sa.ss_family);
continue;
}
addinternaladdr(addr->addr.ifname, &sa, protocol);
}
freeifaddrs(ifap);
if (!isvalidif)
swarnx("cannot find interface/address for %s", addr->addr.ifname);
break;
}
default:
SERRX(addr->atype);
}
}
void
addexternal(addr)
const ruleaddr_t *addr;
{
const char *function = "addexternal()";
ruleaddr_t ra;
int added_ipv4 = 0, added_ipv6 = 0, added_ipv6_gs = 0;
SASSERTX(ntohs(addr->port.tcp) == 0);
SASSERTX(ntohs(addr->port.udp) == 0);
switch (addr->atype) {
case SOCKS_ADDR_DOMAIN: {
/*
* XXX this is not good. It is be better to not resolve this now,
* but resolve it when using. Since we have a hostcache, that
* should not add too much expense. Sending servers a SIGHUP
* when local addresses change is quite common though, so
* assume it's good enough for now.
*/
struct sockaddr_storage sa;
size_t i;
char emsg[1024];
int gaierr;
for (i = 0;
hostname2sockaddr2(addr->addr.domain,
i,
&sa,
&gaierr,
emsg,
sizeof(emsg)) != NULL;
++i) {
SET_SOCKADDRPORT(&sa, addr->port.tcp);
sockaddr2ruleaddr(&sa, &ra);
if (addexternaladdr(&ra) == 0) {
switch (sa.ss_family) {
case AF_INET:
added_ipv4 = 1;
break;
case AF_INET6:
added_ipv6 = 1;
if (!IN6_IS_ADDR_LINKLOCAL(&TOIN6(&sa)->sin6_addr))
added_ipv6_gs = 1;
break;
default:
SERRX(sa.ss_family);
}
}
}
if (i == 0)
yyerrorx("%s", emsg);
break;
}
case SOCKS_ADDR_IPV4:
if (addr->addr.ipv4.ip.s_addr == htonl(INADDR_ANY))
yyerrorx("external address (%s) to connect out from cannot "
"be a wildcard address",
ruleaddr2string(addr, 0, NULL, 0));
ra = *addr;
ra.addr.ipv4.mask.s_addr = htonl(IPV4_FULLNETMASK);
if (addexternaladdr(&ra) == 0)
added_ipv4 = 1;
break;
case SOCKS_ADDR_IPV6:
if (memcmp(&addr->addr.ipv6.ip, &in6addr_any, sizeof(in6addr_any))
== 0)
yyerrorx("external address (%s) cannot be a wildcard address",
ruleaddr2string(addr, 0, NULL, 0));
ra = *addr;
ra.addr.ipv6.maskbits = IPV6_NETMASKBITS;
if (addexternaladdr(&ra) == 0) {
added_ipv6 = 1;
if (!IN6_IS_ADDR_LINKLOCAL(&ra.addr.ipv6.ip))
added_ipv6_gs = 1;
}
break;
case SOCKS_ADDR_IFNAME: {
/*
* Would be nice if this could be cached, e.g. by monitoring a
* routing socket for changes. Have no code for that however.
*/
struct sockaddr_storage sa, t;
size_t i;
/*
* We add the interface, not the addresses. But we want to
* know whether the addresses, at least currently, resolve
* to ipv4 or ipv6 so we can resolve hostnames appropriately.
* E.g., no need to resolve hostname to ipv6 address if we do
* not have ipv6 on the external interface.
*/
for (i = 0;
ifname2sockaddr(addr->addr.ifname, i, &sa, &t) != NULL;
++i) {
const int enabled
= safamily_isenabled(sa.ss_family,
sockaddr2string(&sa, NULL, 0),
EXTERNALIF);
slog(LOG_DEBUG, "%s: ifname %s resolved to address %s. %s",
function,
addr->addr.ifname,
sockaddr2string2(&sa, ADDRINFO_ATYPE, NULL, 0),
enabled ? "enabled" : "not enabled due to address family");
if (!enabled)
continue;
switch (sa.ss_family) {
case AF_INET:
added_ipv4 = 1;
break;
case AF_INET6:
added_ipv6 = 1;
if (!IN6_IS_ADDR_LINKLOCAL(&TOIN6(&sa)->sin6_addr))
added_ipv6_gs = 1;
break;
default:
SERRX(sa.ss_family);
}
}
/*
* Not resolving but adding the ifname itself.
*/
(void)addexternaladdr(addr);
break;
}
default:
SERRX(addr->atype);
}
if (added_ipv4)
add_external_safamily(AF_INET, 1);
if (added_ipv6)
add_external_safamily(AF_INET6, added_ipv6_gs);
}
void
resetconfig(config, exiting)
struct config *config;
const int exiting;
{
const char *function = "resetconfig()";
const int ismainmother = pidismainmother(config->state.pid);
rule_t *rulev[] = { config->crule, config->hrule, config->srule };
monitor_t *monitor;
size_t oldc, i;
slog(LOG_DEBUG, "%s: exiting? %s, ismainmother? %s",
function,
exiting ? "yes" : "no",
ismainmother? "yes" : "no");
if (!exiting) {
#if !HAVE_NO_RESOLVESTUFF
_res.options = config->initial.res_options;
#endif /* !HAVE_NO_RESOLVSTUFF */
}
switch (sockscf.state.type) {
case PROC_MOTHER:
mother_preconfigload();
break;
case PROC_MONITOR:
monitor_preconfigload();
break;
case PROC_NEGOTIATE:
negotiate_preconfigload();
break;
case PROC_REQUEST:
request_preconfigload();
break;
case PROC_IO:
io_preconfigload();
break;
}
/*
* config->initial: nothing to do here.
*/
/*
* Internal interface.
*/
if (config->option.serverc == 1 || !ismainmother) {
/*
* We only support changing these as long as we only have one mother
* process.
* If we are not the main mother, we do need to free the memory as
* usual however, so it will not be leaked when we realloc based on
* the config in shmem that main mother has now installed and from
* which we will update.
*/
free(config->internal.addrv);
config->internal.addrv = NULL;
config->internal.addrc = 0;
bzero(&config->internal.protocol, sizeof(config->internal.protocol));
}
bzero(&config->internal.log, sizeof(config->internal.log));
/*
* External interface.
*/
/* external addresses can always be changed. */
free(config->external.addrv);
config->external.addrv = NULL;
config->external.addrc = 0;
config->external.rotation = ROTATION_NOTSET;
bzero(&config->external.log, sizeof(config->external.log));
bzero(&config->external.protocol, sizeof(config->external.protocol));
/* can always be changed from config. */
bzero(&config->cpu, sizeof(config->cpu));
for (i = 0; i < ELEMENTS(rulev); ++i) {
rule_t *rule, prevrule, *next;
int haveprevrule;
haveprevrule = 0;
rule = rulev[i];
while (rule != NULL) {
/*
* Free normal process-local memory.
*/
int rule_is_autoexpanded;
#if !HAVE_SOCKS_RULES
if (rule->type == object_srule) {
/*
* All pointers are pointers to the same memory in the clientrule,
* so it has already been freed and only the rule itself remains
* to be freed.
*/
next = rule->next;
free(rule);
rule = next;
continue;
}
#endif /* !HAVE_SOCKS_RULES */
if (haveprevrule
&& prevrule.type == rule->type
&& prevrule.number == rule->number) {
slog(LOG_DEBUG,
"%s: another %s with same rule-number (%lu). "
"Must be expanded from the same \"to\"-address",
function,
objecttype2string(rule->type),
(unsigned long)rule->number);
rule_is_autoexpanded = 1;
SASSERTX(BAREFOOTD);
}
else
rule_is_autoexpanded = 0;
if (!rule_is_autoexpanded)
/*
* We don't bother allocating identical memory for auto-expanded
* rules, so only need to free it if it is not auto-expanded.
*/
freelinkedname(rule->user);
rule->user = NULL;
if (!rule_is_autoexpanded)
freelinkedname(rule->group);
rule->group = NULL;
if (!rule_is_autoexpanded)
free(rule->socketoptionv);
rule->socketoptionv = NULL;
rule->socketoptionc = 0;
if (ismainmother) {
/*
* Next go through the shmem in this rule. It's possible
* we have children that are still using, or about to use,
* these segments, so don't delete them now, but save
* them for later. Only upon exit we delete them all.
*
* This means we may have a lot of unneeded shmem segments
* laying around, but since they are just files, rather
* than the, on some systems very scarce, sysv-style shmem
* segments, that should not be any problem. It allows
* us to ignore a lot of nasty locking issues.
*/
size_t moreoldshmemc = 0;
oldshmeminfo_t moreoldshmemv[ 1 /* bw */
+ 1 /* session */
+ 1 /* session state. */
];
if (rule_is_autoexpanded) {
SASSERTX(BAREFOOTD);
SHMEM_CLEAR(rule, SHMEM_ALL, 1);
}
else {
if (rule->bw_shmid != 0) {
moreoldshmemv[moreoldshmemc].id = rule->bw_shmid;
moreoldshmemv[moreoldshmemc].key = key_unset;
moreoldshmemv[moreoldshmemc].type = SHMEM_BW;
++moreoldshmemc;
}
if (rule->ss_shmid != 0) {
/*
* session-module supports statekeys too, so need to save that
* too.
*/
if (sockd_shmat(rule, SHMEM_SS) == 0) {
moreoldshmemv[moreoldshmemc].id = rule->ss_shmid;
moreoldshmemv[moreoldshmemc].key = rule->ss->keystate.key;
moreoldshmemv[moreoldshmemc].type = SHMEM_SS;
++moreoldshmemc;
sockd_shmdt(rule, SHMEM_SS);
}
}
if (moreoldshmemc > 0)
add_more_old_shmem(config, moreoldshmemc, moreoldshmemv);
}
}
prevrule = *rule;
haveprevrule = 1;
next = rule->next;
free(rule);
rule = next;
}
}
config->crule = config->hrule = config->srule = NULL;
/* routeoptions, read from config file. */
bzero(&config->routeoptions, sizeof(config->routeoptions));
/* free routes. */
freeroutelist(config->route);
config->route = NULL;
/* and monitors. */
monitor = sockscf.monitor;
while (monitor != NULL) {
monitor_t *next = monitor->next;
if (ismainmother && monitor->mstats_shmid != 0) {
oldshmeminfo_t moreoldshmemv[ 1 /* just the monitor shmid. */ ];
moreoldshmemv[0].id = monitor->mstats_shmid;
moreoldshmemv[0].key = key_unset;
moreoldshmemv[0].type = SHMEM_MONITOR;
add_more_old_shmem(config, ELEMENTS(moreoldshmemv), moreoldshmemv);
}
free(monitor);
monitor = next;
}
config->monitor = NULL;
/* monitoroptions. Reset on each reload. */
bzero(&config->monitorspec, sizeof(config->monitorspec));
free(config->socketoptionv);
config->socketoptionv = NULL;
config->socketoptionc = 0;
/* compat, read from config file. */
bzero(&config->compat, sizeof(config->compat));
/* extensions, read from config file. */
bzero(&config->extension, sizeof(config->extension));
/*
* log, errlog; handled specially when parsing.
*/
/*
* option; some only settable at commandline, some only read from config
* file. Those only read from config file will be reset to default in
* optioninit().
*/
/* resolveprotocol, read from config file. */
bzero(&config->resolveprotocol, sizeof(config->resolveprotocol));
/*
* socketconfig, read from config file, but also has defaults set by
* optioninit(), so don't need to touch it.
*/
/* srchost, read from config file. */
bzero(&config->srchost, sizeof(config->srchost));
/* stat: not touch. Accumulated continuously. */
/*
* state; keep most of it, with the following exceptions:
*/
/* don't want to have too much code for tracking this, so regen this now. */
config->state.highestfdinuse = 0;
/* timeout, read from config file. */
bzero(&config->timeout, sizeof(config->timeout));
#if HAVE_SOLARIS_PRIVS
/* uid; is special. Needs clearing, but must reopen config-file first. */
#endif /* HAVE_SOLARIS_PRIVS */
/* (child)state: not touched. */
/*
* various method settings. All read from config file.
*/
bzero(config->cmethodv, sizeof(config->cmethodv));
config->cmethodc = 0;
bzero(config->smethodv, sizeof(config->smethodv));
config->smethodc = 0;
/* udpconnectdst. No need to touch. Reset to default on reload. */
#if HAVE_LIBWRAP
if (config->hosts_allow_original != NULL
&& hosts_allow_table != config->hosts_allow_original) {
free(hosts_allow_table);
hosts_allow_table = config->hosts_allow_original;
}
if (config->hosts_deny_original != NULL
&& hosts_deny_table != config->hosts_deny_original) {
free(hosts_deny_table);
hosts_deny_table = config->hosts_deny_original;
}
#endif /* HAVE_LIBWRAP */
if (exiting && ismainmother && config->oldshmemc > 0) {
/*
* Go through the list of saved segments and delete them.
* Any (io) children using them should already have them open,
* and nobody not already using them should need to attach to them
* after we exit. The exception is clients using the session module,
* where we do not keep attached to the segment, but who need to attach
* to it when removing the client. Unfortunately failure to attach
* to a shmem segment is normally a serious error and logged as thus,
* but if mother has removed the segment, then obviously the other
* processes can not attach to it again.
*
* There is some code to only debug log failure to attach to the
* shmem segments (or rather, failure to open the file) if mother
* does not exist (presumably having deleted the files before exiting),
* rather than warn. It depends on mother having exited before the
* child process tries to remove the client though, which may not
* be the case even though we do a little work to increase the odds.
* Worst case is that we end up with some useless warnings though,
* so not worth going overboard with it.
*/
SASSERTX(ismainmother);
SASSERTX(sockscf.state.type == PROC_MOTHER);
/*
* Lock to increase the chance of us having time to exit before
* any children try to attach/detach (they will be blocked waiting for
* the lock). Don't unlock ourselves, but let the kernel release the
* lock when we exit, further reducing gap between us exiting and
* a child process being able to detect it.
*/
socks_lock(config->shmemfd, 0, 0, 1, 1);
slog(LOG_DEBUG, "%s: %ld old shmem entr%s saved. Deleting now",
function, (unsigned long)config->oldshmemc,
config->oldshmemc == 1 ? "y" : "ies");
for (oldc = 0; oldc < config->oldshmemc; ++oldc) {
char fname[PATH_MAX];
snprintf(fname, sizeof(fname), "%s",
sockd_getshmemname(config->oldshmemv[oldc].id, key_unset));
slog(LOG_DEBUG,
"%s: deleting shmem segment shmid %lu in file %s at index #%lu",
function,
(unsigned long)config->oldshmemv[oldc].id,
fname,
(unsigned long)oldc);
if (unlink(fname) != 0)
swarn("%s: failed to unlink shmem segment %ld in file %s",
function, config->oldshmemv[oldc].id, fname);
if (config->oldshmemv[oldc].key != key_unset) {
snprintf(fname, sizeof(fname), "%s",
sockd_getshmemname(config->oldshmemv[oldc].id,
config->oldshmemv[oldc].key));
slog(LOG_DEBUG,
"%s: deleting shmem segment shmid %lu/key %lu in file %s",
function,
(unsigned long)config->oldshmemv[oldc].id,
(unsigned long)config->oldshmemv[oldc].key,
fname);
if (unlink(fname) != 0)
swarn("%s: failed to unlink shmem segment %ld.%d in file %s",
function,
config->oldshmemv[oldc].id,
(int)config->oldshmemv[oldc].key,
fname);
}
}
}
}
void
freeroutelist(routehead)
route_t *routehead;
{
while (routehead != NULL) {
route_t *next = routehead->next;
free(routehead->socketoptionv);
free(routehead);
routehead = next;
}
}
int
addrisbindable(addr)
const ruleaddr_t *addr;
{
const char *function = "addrisbindable()";
struct sockaddr_storage saddr;
int rc, s;
switch (addr->atype) {
case SOCKS_ADDR_IPV4:
case SOCKS_ADDR_IPV6:
sockshost2sockaddr(ruleaddr2sockshost(addr, NULL, SOCKS_TCP), &saddr);
break;
case SOCKS_ADDR_IFNAME: {
struct sockaddr_storage mask;
if (ifname2sockaddr(addr->addr.ifname, 0, &saddr, &mask) == NULL) {
swarn("%s: cannot find interface named %s with ip configured",
function, addr->addr.ifname);
return 0;
}
break;
}
case SOCKS_ADDR_DOMAIN: {
sockshost_t host;
sockshost2sockaddr(ruleaddr2sockshost(addr, &host, SOCKS_TCP), &saddr);
if (!IPADDRISBOUND(&saddr)) {
swarnx("%s can not resolve host %s: %s",
function,
sockshost2string(&host, NULL, 0),
hstrerror(h_errno));
return 0;
}
break;
}
default:
SERRX(addr->atype);
}
if ((s = socket(saddr.ss_family, SOCK_STREAM, 0)) == -1) {
swarn("%s: socket(SOCK_STREAM)", function);
return 0;
}
rc = socks_bind(s, &saddr, 0);
close(s);
if (rc != 0)
swarn("%s: cannot bind address: %s (from address specification %s)",
function,
sockaddr2string(&saddr, NULL, 0),
ruleaddr2string(addr, 0, NULL, 0));
return rc == 0;
}
int
isreplycommandonly(command)
const command_t *command;
{
if ((command->bindreply || command->udpreply)
&& !(command->connect || command->bind || command->udpassociate))
return 1;
else
return 0;
}
int
hasreplycommands(command)
const command_t *command;
{
if (command->bindreply || command->udpreply)
return 1;
else
return 0;
}
ssize_t
addrindex_on_listenlist(listc, listv, _addr, protocol)
const size_t listc;
const listenaddress_t *listv;
const struct sockaddr_storage *_addr;
const int protocol;
{
size_t i;
for (i = 0; i < listc; ++i) {
struct sockaddr_storage addr = *(const struct sockaddr_storage *)_addr;
if (listv[i].protocol != protocol)
continue;
if (GET_SOCKADDRPORT(&addr) == htons(0)) /* match any internal port. */
SET_SOCKADDRPORT(&addr, GET_SOCKADDRPORT(&listv[i].addr));
if (sockaddrareeq(&addr, &listv[i].addr, 0))
return (ssize_t)i;
}
return (ssize_t)-1;
}
ssize_t
addrindex_on_externallist(external, _addr)
const externaladdress_t *external;
const struct sockaddr_storage *_addr;
{
const char *function = "addrindex_on_externallist()";
struct sockaddr_storage sa, addr;
size_t i;
/*
* Not interested in comparing portnumber.
*/
sockaddrcpy(&addr, _addr, sizeof(addr));
SET_SOCKADDRPORT(&addr, htons(0));
slog(LOG_DEBUG,
"%s: checking if address %s is a configured external address",
function, sockaddr2string(&addr, NULL, 0));
for (i = 0; i < external->addrc; ++i) {
slog(LOG_DEBUG, "%s: external address #%lu: %s",
function,
(unsigned long)i,
ruleaddr2string(&external->addrv[i], ADDRINFO_ATYPE, NULL, 0));
switch (external->addrv[i].atype) {
case SOCKS_ADDR_IPV4:
case SOCKS_ADDR_IPV6: {
sockshost_t host;
sockshost2sockaddr(ruleaddr2sockshost(&external->addrv[i],
&host,
SOCKS_TCP),
&sa);
#if DIAGNOSTIC
SASSERTX(safamily_isenabled(sa.ss_family,
sockaddr2string(&sa, NULL, 0),
EXTERNALIF));
#endif /* DIAGNOSTIC */
if (sockaddrareeq(&addr, &sa, 0))
return (ssize_t)i;
break;
}
case SOCKS_ADDR_DOMAIN: {
char emsg[1024];
int gaierr;
size_t ii;
ii = 0;
while (hostname2sockaddr2(external->addrv[i].addr.domain,
ii++,
&sa,
&gaierr,
emsg,
sizeof(emsg)) != NULL) {
slog(LOG_DEBUG, "%s: checking resolved address %s ...",
function, sockaddr2string(&sa, NULL, 0));
if (!safamily_isenabled(sa.ss_family,
sockaddr2string(&sa, NULL, 0),
EXTERNALIF))
continue;
if (sockaddrareeq(&addr, &sa, 0))
return (ssize_t)i;
}
if (ii == 0)
swarnx("%s: problem with address on external interface: %s",
function, emsg);
break;
}
case SOCKS_ADDR_IFNAME: {
struct sockaddr_storage mask;
size_t ii;
ii = 0;
while (ifname2sockaddr(external->addrv[i].addr.ifname,
ii++,
&sa,
&mask) != NULL) {
if (!safamily_isenabled(sa.ss_family,
sockaddr2string(&sa, NULL, 0),
EXTERNALIF))
continue;
if (sockaddrareeq(&addr, &sa, 0))
return (ssize_t)i;
}
break;
}
default:
SERRX(external->addrv[i].atype);
}
}
return (ssize_t)-1;
}
void
checkconfig(void)
{
const char *function = "checkconfig()";
#if HAVE_PAM
char *pamservicename = NULL;
#endif /* HAVE_PAM */
#if HAVE_BSDAUTH
char *bsdauthstylename = NULL;
#endif /* HAVE_BSDAUTH */
#if HAVE_GSSAPI
char *gssapiservicename = NULL, *gssapikeytab = NULL;
#endif /* HAVE_GSSAPI */
/* XXX add same for LDAP */
rule_t *rulebasev[] = { sockscf.crule,
sockscf.hrule,
sockscf.srule
};
#if HAVE_PAM || HAVE_BSDAUTH || HAVE_GSSAPI
int *methodbasev[] = { sockscf.cmethodv,
sockscf.cmethodv,
sockscf.smethodv
};
size_t *methodbasec[] = { &sockscf.cmethodc,
&sockscf.cmethodc,
&sockscf.smethodc
};
#endif /* HAVE_PAM || HAVE_BSDAUTH || HAVE_GSSAPI */
size_t i, basec;
int usinglibwrap = 0;
for (i = 0; i < sockscf.cmethodc; ++i) {
SASSERTX(sockscf.cmethodv[i] >= AUTHMETHOD_NONE);
SASSERTX(sockscf.cmethodv[i] <= AUTHMETHOD_MAX);
SASSERTX(methodisvalid(sockscf.cmethodv[i], object_crule));
if (sockscf.cmethodv[i] == AUTHMETHOD_RFC931)
usinglibwrap = 1;
}
#if HAVE_SOCKS_RULES
if (sockscf.smethodc == 0)
swarnx("%s: no socks authentication methods enabled. This means all "
"socks requests will be blocked after negotiation. "
"Perhaps this is not intended?",
function);
else {
for (i = 0; i < sockscf.smethodc; ++i) {
SASSERTX(sockscf.smethodv[i] >= AUTHMETHOD_NONE);
SASSERTX(sockscf.smethodv[i] <= AUTHMETHOD_MAX);
if (sockscf.smethodv[i] == AUTHMETHOD_RFC931)
usinglibwrap = 1;
if (sockscf.smethodv[i] == AUTHMETHOD_NONE
&& i + 1 < sockscf.smethodc)
yywarnx("authentication method \"%s\" is configured in the "
"global socksmethod list, but since authentication "
"methods are selected by the priority given, we will "
"never try to match any of the subsequent authentication "
"methods. I.e., no match will ever be attempted on the "
"next method, method \"%s\"",
method2string(sockscf.smethodv[i]),
method2string(sockscf.smethodv[i + 1]));
}
}
#endif /* HAVE_SOCKS_RULES */
/*
* Check rules, including if some rule-specific settings vary across
* rules. If they don't, we can optimize things when running.
*/
basec = 0;
while (basec < ELEMENTS(rulebasev)) {
rule_t *rule = rulebasev[basec++];
if (rule == NULL)
continue;
for (; rule != NULL; rule = rule->next) {
size_t methodc;
int *methodv;
#if HAVE_LIBWRAP
if (*rule->libwrap != NUL)
usinglibwrap = 1;
#endif /* HAVE_LIBWRAP */
/*
* What methods do we need to check? clientmethods for
* client-rules, socksmethods for socks-rules.
*/
switch (rule->type) {
case object_crule:
#if HAVE_SOCKS_HOSTID
case object_hrule:
#endif /* HAVE_SOCKS_HOSTID */
methodc = rule->state.cmethodc;
methodv = rule->state.cmethodv;
break;
case object_srule:
methodc = rule->state.smethodc;
methodv = rule->state.smethodv;
break;
default:
SERRX(rule->type);
}
for (i = 0; i < methodc; ++i) {
switch (methodv[i]) {
#if HAVE_PAM
case AUTHMETHOD_PAM_ANY:
case AUTHMETHOD_PAM_ADDRESS:
case AUTHMETHOD_PAM_USERNAME:
if (*sockscf.state.pamservicename == NUL)
break; /* already found to vary. */
if (pamservicename == NULL) /* first pam rule. */
pamservicename = rule->state.pamservicename;
else if (strcmp(pamservicename, rule->state.pamservicename)
!= 0) {
slog(LOG_DEBUG, "%s: pam.servicename varies, %s ne %s",
function,
pamservicename,
rule->state.pamservicename);
*sockscf.state.pamservicename = NUL;
}
break;
#endif /* HAVE_PAM */
#if HAVE_BSDAUTH
case AUTHMETHOD_BSDAUTH:
if (*sockscf.state.bsdauthstylename == NUL)
break; /* already found to vary. */
if (bsdauthstylename == NULL) /* first bsdauth rule. */
bsdauthstylename = rule->state.bsdauthstylename;
else if (strcmp(bsdauthstylename,
rule->state.bsdauthstylename) != 0) {
slog(LOG_DEBUG,
"%s: bsdauth.stylename varies, %s ne %s",
function,
bsdauthstylename,
rule->state.bsdauthstylename);
*sockscf.state.bsdauthstylename = NUL;
}
break;
#endif /* HAVE_BSDAUTH */
#if HAVE_GSSAPI
case AUTHMETHOD_GSSAPI:
if (*sockscf.state.gssapiservicename != NUL) {
if (gssapiservicename == NULL) /* first gssapi rule. */
gssapiservicename = rule->state.gssapiservicename;
else if (strcmp(gssapiservicename,
rule->state.gssapiservicename) != 0) {
slog(LOG_DEBUG,
"%s: gssapi.servicename varies, %s ne %s",
function,
gssapiservicename,
rule->state.gssapiservicename);
*sockscf.state.gssapiservicename = NUL;
}
}
/* else; already found to vary. */
if (*sockscf.state.gssapikeytab != NUL) {
if (gssapikeytab == NULL) /* first gssapi rule. */
gssapikeytab = rule->state.gssapikeytab;
else if (strcmp(gssapikeytab, rule->state.gssapikeytab)
!= 0) {
slog(LOG_DEBUG, "%s: gssapi.keytab varies, %s ne %s",
function,
gssapikeytab,
rule->state.gssapikeytab);
*sockscf.state.gssapikeytab = NUL;
}
}
/* else; already found to vary. */
break;
#endif /* HAVE_GSSAPI */
default:
break;
}
}
#if BAREFOOTD
if (rule->type == object_crule) {
if (rule->state.protocol.tcp)
/*
* Add all "to:" addresses to the list of internal interfaces;
* barefootd doesn't use a separate "internal:" keyword for it.
*/
addinternal(&rule->dst, SOCKS_TCP);
if (rule->state.protocol.udp)
sockscf.state.alludpbounced = 0;
}
#endif /* BAREFOOTD */
}
}
/*
* Check that the main configured privileges work.
*/
sockd_priv(SOCKD_PRIV_PRIVILEGED, PRIV_ON);
sockd_priv(SOCKD_PRIV_PRIVILEGED, PRIV_OFF);
sockd_priv(SOCKD_PRIV_UNPRIVILEGED, PRIV_ON);
sockd_priv(SOCKD_PRIV_UNPRIVILEGED, PRIV_OFF);
sockd_priv(SOCKD_PRIV_LIBWRAP, PRIV_ON);
sockd_priv(SOCKD_PRIV_LIBWRAP, PRIV_OFF);
#if !HAVE_PRIVILEGES
SASSERTX(sockscf.state.euid == geteuid());
SASSERTX(sockscf.state.egid == getegid());
if (sockscf.uid.unprivileged_uid == 0)
swarnx("%s: setting the unprivileged uid to %d is not recommended "
"for security reasons",
function, sockscf.uid.unprivileged_uid);
#if HAVE_LIBWRAP
if (usinglibwrap && sockscf.uid.libwrap_uid == 0)
swarnx("%s: setting the libwrap uid to %d is almost never needed, and "
"is not recommended for security reasons",
function, sockscf.uid.libwrap_uid);
#endif /* HAVE_LIBWRAP */
#endif /* !HAVE_PRIVILEGES */
#if HAVE_PAM
if (*sockscf.state.pamservicename != NUL
&& pamservicename != NULL) {
/*
* pamservicename does not vary, but is not necessarily the
* the same as sockscf.state.pamservicename (default).
* If it is not, set sockscf.state.pamservicename to
* what the user used in one or more of the rules, since
* it is the same in all rules, i.e. making it that value
* we use to make passworddbisunique() work as expected.
*
* Likewise for bsdauth, gssapi, etc.
*/
if (strcmp(pamservicename, sockscf.state.pamservicename) != 0)
STRCPY_CHECKLEN(sockscf.state.pamservicename,
pamservicename,
sizeof(sockscf.state.pamservicename) - 1,
yyerrorx);
}
#endif /* HAVE_PAM */
#if HAVE_BSDAUTH
if (*sockscf.state.bsdauthstylename != NUL
&& bsdauthstylename != NULL) {
if (strcmp(bsdauthstylename, sockscf.state.bsdauthstylename) != 0)
STRCPY_CHECKLEN(sockscf.state.bsdauthstylename,
bsdauthstylename,
sizeof(sockscf.state.bsdauthstylename) - 1,
yyerrorx);
}
#endif /* HAVE_BSDAUTH */
#if HAVE_GSSAPI
if (*sockscf.state.gssapiservicename != NUL
&& gssapiservicename != NULL) {
if (strcmp(gssapiservicename, sockscf.state.gssapiservicename) != 0)
STRCPY_CHECKLEN(sockscf.state.gssapiservicename,
gssapiservicename,
sizeof(sockscf.state.gssapiservicename) - 1,
yyerrorx);
}
if (*sockscf.state.gssapikeytab != NUL
&& gssapikeytab != NULL) {
if (strcmp(gssapikeytab, sockscf.state.gssapikeytab) != 0)
STRCPY_CHECKLEN(sockscf.state.gssapikeytab,
gssapikeytab,
sizeof(sockscf.state.gssapikeytab) - 1,
yyerrorx);
}
#endif /* HAVE_GSSAPI */
/*
* Go through all rules again and set default values for
* authentication-methods based on the global method-lines, if none set.
*/
basec = 0;
while (basec < ELEMENTS(rulebasev)) {
#if HAVE_PAM || HAVE_BSDAUTH || HAVE_GSSAPI
const int *methodv = methodbasev[basec];
const size_t methodc = *methodbasec[basec];
#endif /* HAVE_PAM || HAVE_BSDAUTH || HAVE_GSSAPI */
rule_t *rule = rulebasev[basec];
++basec;
if (rule == NULL)
continue;
for (; rule != NULL; rule = rule->next) {
#if HAVE_PAM
if (methodisset(AUTHMETHOD_PAM_ANY, methodv, methodc)
|| methodisset(AUTHMETHOD_PAM_ADDRESS, methodv, methodc)
|| methodisset(AUTHMETHOD_PAM_USERNAME, methodv, methodc))
if (*rule->state.pamservicename == NUL) /* set to default. */
STRCPY_ASSERTSIZE(rule->state.pamservicename,
sockscf.state.pamservicename);
#endif /* HAVE_PAM */
#if HAVE_BSDAUTH
if (methodisset(AUTHMETHOD_BSDAUTH, methodv, methodc))
if (*rule->state.bsdauthstylename == NUL) { /* set to default. */
if (*sockscf.state.bsdauthstylename != NUL)
STRCPY_ASSERTSIZE(rule->state.bsdauthstylename,
sockscf.state.bsdauthstylename);
}
#endif /* HAVE_BSDAUTH */
#if HAVE_GSSAPI
if (methodisset(AUTHMETHOD_GSSAPI, methodv, methodc)) {
if (*rule->state.gssapiservicename == NUL) /* set to default. */
STRCPY_ASSERTSIZE(rule->state.gssapiservicename,
sockscf.state.gssapiservicename);
if (*rule->state.gssapikeytab == NUL) /* set to default. */
STRCPY_ASSERTSIZE(rule->state.gssapikeytab,
sockscf.state.gssapikeytab);
/*
* can't do memcmp since we don't want to include
* gssapiencryption.nec in the compare.
*/
if (rule->state.gssapiencryption.clear == 0
&& rule->state.gssapiencryption.integrity == 0
&& rule->state.gssapiencryption.confidentiality == 0
&& rule->state.gssapiencryption.permessage == 0) {
rule->state.gssapiencryption.clear = 1;
rule->state.gssapiencryption.integrity = 1;
rule->state.gssapiencryption.confidentiality = 1;
rule->state.gssapiencryption.permessage = 0;
}
}
#endif /* HAVE_GSSAPI */
#if HAVE_LDAP
if (*rule->state.ldap.keytab == NUL) /* set to default. */
STRCPY_ASSERTSIZE(rule->state.ldap.keytab, DEFAULT_GSSAPIKEYTAB);
if (*rule->state.ldap.filter == NUL) /* set to default. */
STRCPY_ASSERTSIZE(rule->state.ldap.filter, DEFAULT_LDAP_FILTER);
if (*rule->state.ldap.filter_AD == NUL) /* set to default. */
STRCPY_ASSERTSIZE(rule->state.ldap.filter_AD,
DEFAULT_LDAP_FILTER_AD);
if (*rule->state.ldap.attribute == NUL) /* set to default. */
STRCPY_ASSERTSIZE(rule->state.ldap.attribute,
DEFAULT_LDAP_ATTRIBUTE);
if (*rule->state.ldap.attribute_AD == NUL) /* set to default. */
STRCPY_ASSERTSIZE(rule->state.ldap.attribute_AD,
DEFAULT_LDAP_ATTRIBUTE_AD);
if (*rule->state.ldap.certfile == NUL) /* set to default. */
STRCPY_ASSERTSIZE(rule->state.ldap.certfile,
DEFAULT_LDAP_CACERTFILE);
if (*rule->state.ldap.certpath == NUL) /* set to default. */
STRCPY_ASSERTSIZE(rule->state.ldap.certpath,
DEFAULT_LDAP_CERTDBPATH);
if (rule->state.ldap.port == 0) /* set to default */
rule->state.ldap.port = SOCKD_EXPLICIT_LDAP_PORT;
if (rule->state.ldap.portssl == 0) /* set to default */
rule->state.ldap.portssl = SOCKD_EXPLICIT_LDAPS_PORT;
#endif /* HAVE_LDAP */
}
}
#if BAREFOOTD
if (sockscf.internal.addrc == 0 && ALL_UDP_BOUNCED())
serrx("%s: no client-rules to accept clients on specified", function);
#else /* !BAREFOOTD */
if (sockscf.internal.addrc == 0)
serrx("%s: no internal address given for server to listen for clients on",
function);
#endif /* !BAREFOOTD */
if (sockscf.external.addrc == 0)
serrx("%s: no external address specified for server to use when "
"forwarding data on behalf of clients",
function);
if (sockscf.external.rotation == ROTATION_NONE) {
size_t ipv4c = 0, ipv6c = 0;
for (i = 0; i < sockscf.external.addrc; ++i) {
switch (sockscf.external.addrv[i].atype) {
case SOCKS_ADDR_IPV4:
++ipv4c;
break;
case SOCKS_ADDR_IPV6:
++ipv6c;
break;
case SOCKS_ADDR_IFNAME:
case SOCKS_ADDR_DOMAIN:
break;
default:
SERRX(sockscf.external.addrv[i].atype);
}
}
if (ipv4c > 1 || ipv6c > 1)
swarnx("%s: more than one external address has been specified, but "
"as long as external.rotation has the default value %s "
"only the first address specified will be used",
function, rotation2string(sockscf.external.rotation));
}
if (sockscf.external.rotation == ROTATION_SAMESAME
&& sockscf.external.addrc == 1)
swarnx("%s: rotation for external addresses is set to same-same, but "
"the number of external addresses is only one, so this does "
"not make sense",
function);
if (sockscf.routeoptions.maxfail == 0 && sockscf.routeoptions.badexpire != 0)
swarnx("%s: it does not make sense to set \"route.badexpire\" "
"when \"route.maxfail\" is set to zero",
function);
#if COVENANT
if (*sockscf.realmname == NUL)
STRCPY_ASSERTSIZE(sockscf.realmname, DEFAULT_REALMNAME);
#endif /* COVENANT */
#if HAVE_SCHED_SETAFFINITY
{
const cpusetting_t *cpuv[] = { &sockscf.cpu.mother,
&sockscf.cpu.monitor,
&sockscf.cpu.negotiate,
&sockscf.cpu.request,
&sockscf.cpu.io };
const int proctypev[] = { PROC_MOTHER,
PROC_MONITOR,
PROC_NEGOTIATE,
PROC_REQUEST,
PROC_IO };
size_t i;
for (i = 0; i < ELEMENTS(cpuv); ++i)
if (cpuv[i]->affinity_isset && !sockd_cpuset_isok(&cpuv[i]->mask))
serrx("%s: invalid cpu mask configured for %s process: %s",
function,
childtype2string(proctypev[i]),
cpuset2string(&cpuv[i]->mask, NULL, 0));
}
#endif /* HAVE_SCHED_SETAFFINITY */
for (i = 0; i < sockscf.external.addrc; ++i)
if (!addrisbindable(&sockscf.external.addrv[i]))
serrx("%s: cannot bind external address #%ld: %s",
function,
(long)i,
ruleaddr2string(&sockscf.external.addrv[i], 0, NULL, 0));
}
void
add_external_safamily(safamily, globalscope)
const sa_family_t safamily;
const int globalscope;
{
switch (safamily) {
case AF_INET:
sockscf.external.protocol.hasipv4 = 1;
break;
case AF_INET6:
sockscf.external.protocol.hasipv6 = 1;
if (globalscope)
sockscf.external.protocol.hasipv6_globalscope = 1;
break;
default:
SERRX(safamily);
}
}
int
external_has_safamily(safamily)
const sa_family_t safamily;
{
const char *function = "external_has_safamily()";
SASSERTX(sockscf.shmeminfo != NULL);
#if 0
slog(LOG_DEBUG, "%s: hasipv4: %d, hasipv6: %d, hasipv6_globalscope: %d",
function,
sockscf.shmeminfo->state.external_hasipv4,
sockscf.shmeminfo->state.external_hasipv6,
sockscf.shmeminfo->state.external_hasipv6_globalscope);
#endif
switch (safamily) {
case AF_INET:
return sockscf.external.protocol.hasipv4;
case AF_INET6:
return sockscf.external.protocol.hasipv6;
default:
SERRX(safamily);
}
}
void
add_internal_safamily(safamily)
const sa_family_t safamily;
{
switch (safamily) {
case AF_INET:
sockscf.internal.protocol.hasipv4 = 1;
break;
case AF_INET6:
sockscf.internal.protocol.hasipv6 = 1;
break;
default:
SERRX(safamily);
}
}
int
internal_has_safamily(safamily)
const sa_family_t safamily;
{
const char *function = "internal_has_safamily()";
SASSERTX(sockscf.shmeminfo != NULL);
#if 0
slog(LOG_DEBUG, "%s: hasipv4: %d, hasipv6: %d",
function,
sockscf.internal.protocol.hasipv4,
sockscf.internal.protocol.hasipv6);
#endif
switch (safamily) {
case AF_INET:
return sockscf.internal.protocol.hasipv4;
case AF_INET6:
return sockscf.internal.protocol.hasipv6;
default:
SERRX(safamily);
}
}
int
external_has_only_safamily(safamily)
const sa_family_t safamily;
{
switch (safamily) {
case AF_INET:
return (external_has_safamily(AF_INET)
&& !external_has_safamily(AF_INET6));
case AF_INET6:
return (external_has_safamily(AF_INET6)
&& !external_has_safamily(AF_INET));
default:
SERRX(safamily);
}
}
int
external_has_global_safamily(safamily)
const sa_family_t safamily;
{
SASSERTX(sockscf.shmeminfo != NULL);
switch (safamily) {
case AF_INET: /* don't care about scope for IPv4. */
return sockscf.external.protocol.hasipv4;
case AF_INET6:
return sockscf.external.protocol.hasipv6_globalscope;
default:
SERRX(safamily);
}
}
static void
add_more_old_shmem(config, memc, memv)
struct config *config;
const size_t memc;
const oldshmeminfo_t memv[];
{
const char *function = "add_more_old_shmem()";
void *old;
size_t i;
if ((old = realloc(config->oldshmemv,
sizeof(*config->oldshmemv) * (config->oldshmemc + memc)))
== NULL) {
swarn("%s: could not allocate %lu bytes of memory to "
"hold old shmids for later removal",
function,
(unsigned long)(sizeof(*config->oldshmemv)
* (config->oldshmemc + memc)));
return;
}
config->oldshmemv = old;
for (i = 0; i < memc; ++i) {
const char *type;
switch (memv[i].type) {
case SHMEM_BW:
type = "bw";
break;
case SHMEM_MONITOR:
type = "monitor";
break;
case SHMEM_SS:
type = "session";
break;
default:
SERRX(memv[i].type);
}
slog(LOG_DEBUG,
"%s: saving old shmem-object of type %lu (%s), with "
"shmid %lu/key %lu, at index #%lu, for removal upon exit",
function,
(unsigned long)memv[i].type,
type,
(unsigned long)memv[i].id,
(unsigned long)memv[i].key,
(unsigned long)i);
config->oldshmemv[config->oldshmemc++] = memv[i];
}
}
static int
safamily_isenabled(safamily, addrstr, side)
const sa_family_t safamily;
const char *addrstr;
const interfaceside_t side;
{
const char *function = "safamily_isenabled()";
interfaceprotocol_t *interface;
int isenabled;
switch (side) {
case INTERNALIF:
interface = &sockscf.internal.protocol;
break;
case EXTERNALIF:
interface = &sockscf.external.protocol;
break;
default:
SERRX(side);
}
isenabled = 0;
switch (safamily) {
case AF_INET:
if (interface->ipv4)
isenabled = 1;
break;
case AF_INET6:
if (interface->ipv6)
isenabled = 1;
break;
default:
SERRX(safamily);
}
if (!isenabled)
slog(LOG_DEBUG,
"%s: address family %s option is not enabled on the %s-side "
"interface. Can not use address \"%s\"",
function,
safamily2string(safamily),
interfaceside2string(side),
addrstr);
return isenabled;
}
static int
addexternaladdr(ra)
const struct ruleaddr_t *ra;
{
void *old;
switch (ra->atype) {
case SOCKS_ADDR_IPV4:
case SOCKS_ADDR_IPV6:
if (!safamily_isenabled(atype2safamily(ra->atype),
ruleaddr2string(ra, ADDRINFO_ATYPE, NULL, 0),
EXTERNALIF))
return -1;
break;
default:
/*
* Will be resolved when needed.
*/
break;
}
old = realloc(sockscf.external.addrv,
sizeof(*sockscf.external.addrv) * (sockscf.external.addrc + 1));
if (old == NULL)
yyerror(NOMEM);
sockscf.external.addrv = old;
sockscf.external.addrv[sockscf.external.addrc++] = *ra;
return 0;
}
static int
addinternaladdr(ifname, sa, protocol)
const char *ifname;
const struct sockaddr_storage *sa;
const int protocol;
{
const char *function = "addinternaladdr()";
void *old;
if (!safamily_isenabled(sa->ss_family,
sockaddr2string2(sa, ADDRINFO_ATYPE, NULL, 0),
INTERNALIF) != 0)
return -1;
slog(LOG_DEBUG, "%s: adding address %s on nic %s to the internal list",
function, sockaddr2string(sa, NULL, 0), ifname);
old = realloc(sockscf.internal.addrv,
sizeof(*sockscf.internal.addrv) * (sockscf.internal.addrc + 1));
if (old == NULL)
yyerror(NOMEM);
sockscf.internal.addrv = old;
#if 0
bzero(&sockscf.internal.addrv[sockscf.internal.addrc],
sizeof(sockscf.internal.addrv[sockscf.internal.addrc]));
#endif
sockaddrcpy(&sockscf.internal.addrv[sockscf.internal.addrc].addr,
sa,
sizeof(sockscf.internal.addrv[sockscf.internal.addrc].addr));
sockscf.internal.addrv[sockscf.internal.addrc].protocol = protocol;
sockscf.internal.addrv[sockscf.internal.addrc].s = -1;
++sockscf.internal.addrc;
add_internal_safamily(sa->ss_family);
return 0;
}
|