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 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349
|
/* $OpenBSD: conf.c,v 1.55 2003/06/03 14:28:16 ho Exp $ */
/* $EOM: conf.c,v 1.48 2000/12/04 02:04:29 angelos Exp $ */
/*
* Copyright (c) 1998, 1999, 2000, 2001 Niklas Hallqvist. All rights reserved.
* Copyright (c) 2000, 2001, 2002 Hkan Olsson. 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. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 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.
*/
/*
* This code was written under funding by Ericsson Radio Systems.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <sys/param.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <err.h>
#include <syslog.h>
#include <libgen.h>
#include <sys/file.h>
#include <time.h>
#include <dirent.h>
#include "conffile.h"
#include "xlog.h"
#define CONF_FILE_EXT ".conf"
#define CONF_FILE_EXT_LEN ((int) (sizeof(CONF_FILE_EXT) - 1))
#pragma GCC visibility push(hidden)
static void conf_load_defaults(void);
static char * conf_readfile(const char *path);
static int conf_set(int , const char *, const char *, const char *,
const char *, int , int );
static void conf_parse(int trans, char *buf,
char **section, char **subsection, const char *filename);
struct conf_trans {
TAILQ_ENTRY (conf_trans) link;
int trans;
enum conf_op { CONF_SET, CONF_REMOVE, CONF_REMOVE_SECTION } op;
char *section;
char *arg;
char *tag;
char *value;
int override;
int is_default;
};
TAILQ_HEAD (conf_trans_head, conf_trans) conf_trans_queue;
/*
* Radix-64 Encoding.
*/
static const uint8_t asc2bin[] =
{
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 62, 255, 255, 255, 63,
52, 53, 54, 55, 56, 57, 58, 59,
60, 61, 255, 255, 255, 255, 255, 255,
255, 0, 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, 255, 255, 255, 255, 255,
255, 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, 255, 255, 255, 255, 255
};
struct conf_binding {
LIST_ENTRY (conf_binding) link;
char *section;
char *arg;
char *tag;
char *value;
int is_default;
};
LIST_HEAD (conf_bindings, conf_binding) conf_bindings[256];
const char *modified_by = NULL;
static __inline__ uint8_t
conf_hash(const char *s)
{
uint8_t hash = 0;
while (*s) {
hash = ((hash << 1) | (hash >> 7)) ^ tolower (*s);
s++;
}
return hash;
}
/*
* free all the component parts of a conf_binding struct
*/
static void free_confbind(struct conf_binding *cb)
{
if (!cb)
return;
if (cb->section)
free(cb->section);
if (cb->arg)
free(cb->arg);
if (cb->tag)
free(cb->tag);
if (cb->value)
free(cb->value);
free(cb);
}
static void free_conftrans(struct conf_trans *ct)
{
if (!ct)
return;
if (ct->section)
free(ct->section);
if (ct->arg)
free(ct->arg);
if (ct->tag)
free(ct->tag);
if (ct->value)
free(ct->value);
free(ct);
}
/*
* Insert a tag-value combination from LINE (the equal sign is at POS)
*/
static int
conf_remove_now(const char *section, const char *arg, const char *tag)
{
struct conf_binding *cb, *next;
cb = LIST_FIRST(&conf_bindings[conf_hash (section)]);
for (; cb; cb = next) {
next = LIST_NEXT(cb, link);
if (arg && (cb->arg == NULL || strcasecmp(arg, cb->arg) != 0))
continue;
if (strcasecmp(cb->section, section) == 0
&& strcasecmp(cb->tag, tag) == 0) {
LIST_REMOVE(cb, link);
xlog(LOG_INFO,"[%s]:%s->%s removed", section, tag, cb->value);
free_confbind(cb);
return 0;
}
}
return 1;
}
static int
conf_remove_section_now(const char *section)
{
struct conf_binding *cb, *next;
int unseen = 1;
cb = LIST_FIRST(&conf_bindings[conf_hash (section)]);
for (; cb; cb = next) {
next = LIST_NEXT(cb, link);
if (strcasecmp(cb->section, section) == 0) {
unseen = 0;
LIST_REMOVE(cb, link);
xlog(LOG_INFO, "[%s]:%s->%s removed", section, cb->tag, cb->value);
free_confbind(cb);
}
}
return unseen;
}
/*
* Insert a tag-value combination from LINE (the equal sign is at POS)
* into SECTION of our configuration database.
*/
static int
conf_set_now(const char *section, const char *arg, const char *tag,
const char *value, int override, int is_default)
{
struct conf_binding *node = 0;
if (override)
conf_remove_now(section, arg, tag);
else if (conf_get_section(section, arg, tag)) {
if (!is_default) {
xlog(LOG_INFO, "conf_set: duplicate tag [%s]:%s, ignoring...",
section, tag);
}
return 1;
}
node = calloc(1, sizeof *node);
if (!node) {
xlog_warn("conf_set: calloc (1, %lu) failed", (unsigned long)sizeof *node);
return 1;
}
node->section = strdup(section);
if (arg)
node->arg = strdup(arg);
node->tag = strdup(tag);
node->value = strdup(value);
node->is_default = is_default;
LIST_INSERT_HEAD(&conf_bindings[conf_hash (section)], node, link);
return 0;
}
/* Attempt to construct a relative path to the new file */
static char *
relative_path(const char *oldfile, const char *newfile)
{
char *tmpcopy = NULL;
char *dir = NULL;
char *relpath = NULL;
size_t pathlen;
if (!newfile)
return strdup(oldfile);
if (newfile[0] == '/')
return strdup(newfile);
tmpcopy = strdup(oldfile);
if (!tmpcopy)
goto mem_err;
dir = dirname(tmpcopy);
pathlen = strlen(dir) + strlen(newfile) + 2;
relpath = calloc(1, pathlen);
if (!relpath)
goto mem_err;
snprintf(relpath, pathlen, "%s/%s", dir, newfile);
free(tmpcopy);
return relpath;
mem_err:
if (tmpcopy)
free(tmpcopy);
return NULL;
}
/*
* Parse the line LINE of SZ bytes. Skip Comments, recognize section
* headers and feed tag-value pairs into our configuration database.
*/
static void
conf_parse_line(int trans, char *line, const char *filename, int lineno, char **section, char **subsection)
{
char *val, *ptr;
char *inc_section = NULL, *inc_subsection = NULL;
char *relpath, *subconf;
/* Strip off any leading blanks */
while (isspace(*line))
line++;
/* Ignore blank lines */
if (*line == '\0')
return;
/* Lines starting with '#' or ';' are comments. */
if (*line == '#' || *line == ';')
return;
/* '[section]' parsing... */
if (*line == '[') {
line++;
if (*section) {
free(*section);
*section = NULL;
}
if (*subsection) {
free(*subsection);
*subsection = NULL;
}
/* Strip off any blanks after '[' */
while (isblank(*line))
line++;
/* find the closing ] */
ptr = strchr(line, ']');
if (ptr == NULL) {
xlog_warn("config error at %s:%d: "
"non-matched ']', ignoring until next section",
filename, lineno);
return;
}
/* just ignore everything after the closing ] */
*(ptr--) = '\0';
/* Strip off any blanks before ']' */
while (ptr >= line && isblank(*ptr))
*(ptr--)='\0';
/* look for an arg to split from the section name */
val = strchr(line, '"');
if (val != NULL) {
ptr = val - 1;
*(val++) = '\0';
/* trim away any whitespace before the " */
while (ptr > line && isblank(*ptr))
*(ptr--)='\0';
}
/* copy the section name */
*section = strdup(line);
if (!*section) {
xlog_warn("config error at %s:%d:"
"malloc failed", filename, lineno);
return;
}
/* there is no arg, we are done */
if (val == NULL)
return;
/* check for the closing " */
ptr = strchr(val, '"');
if (ptr == NULL) {
xlog_warn("config error at %s:%d: "
"non-matched '\"', ignoring until next section",
filename, lineno);
return;
}
*ptr = '\0';
*subsection = strdup(val);
if (!*subsection)
xlog_warn("config error at %s:%d: "
"malloc failed", filename, lineno);
return;
}
/* Deal with assignments. */
ptr = strchr(line, '=');
/* not an assignment line */
if (ptr == NULL) {
/* Other non-empty lines are weird. */
if (line[strspn(line, " \t")])
xlog_warn("config error at %s:%d: "
"line not empty and not an assignment",
filename, lineno);
return;
}
/* If no section, we are ignoring the line. */
if (!*section) {
xlog_warn("config error at %s:%d: "
"ignoring line not in a section",
filename, lineno);
return;
}
val = ptr + 1;
*(ptr--) = '\0';
/* strip spaces before and after the = */
while (ptr >= line && isblank(*ptr))
*(ptr--)='\0';
while (*val != '\0' && isblank(*val))
val++;
if (*val == '"') {
val++;
ptr = strchr(val, '"');
if (ptr == NULL) {
xlog_warn("config error at %s:%d: "
"unmatched quotes",filename, lineno);
return;
}
*ptr = '\0';
} else
if (*val == '\'') {
val++;
ptr = strchr(val, '\'');
if (ptr == NULL) {
xlog_warn("config error at %s:%d: "
"unmatched quotes", filename, lineno);
return;
}
*ptr = '\0';
} else {
/* Trim any trailing spaces and comments */
if ((ptr=strchr(val, '#'))!=NULL)
*ptr = '\0';
if ((ptr=strchr(val, ';'))!=NULL)
*ptr = '\0';
ptr = val + strlen(val) - 1;
while (ptr > val && isspace(*ptr))
*(ptr--) = '\0';
}
if (*line == '\0') {
xlog_warn("config error at %s:%d: "
"missing tag in assignment", filename, lineno);
return;
}
if (strcasecmp(line, "include")==0) {
/* load and parse subordinate config files */
_Bool optional = false;
if (val && *val == '-') {
optional = true;
val++;
}
relpath = relative_path(filename, val);
if (relpath == NULL) {
if (!optional)
xlog_warn("config error at %s:%d: error loading included config",
filename, lineno);
return;
}
subconf = conf_readfile(relpath);
if (subconf == NULL) {
if (!optional)
xlog_warn("config error at %s:%d: error loading included config",
filename, lineno);
if (relpath)
free(relpath);
return;
}
/* copy the section data so the included file can inherit it
* without accidentally changing it for us */
if (*section != NULL) {
inc_section = strdup(*section);
if (*subsection != NULL)
inc_subsection = strdup(*subsection);
}
conf_parse(trans, subconf, &inc_section, &inc_subsection, relpath);
if (inc_section)
free(inc_section);
if (inc_subsection)
free(inc_subsection);
if (relpath)
free(relpath);
free(subconf);
} else {
/* XXX Perhaps should we not ignore errors? */
conf_set(trans, *section, *subsection, line, val, 1, 0);
}
}
/* Parse the mapped configuration file. */
static void
conf_parse(int trans, char *buf, char **section, char **subsection, const char *filename)
{
char *cp = buf;
char *bufend = NULL;
char *line;
int lineno = 0;
line = cp;
bufend = buf + strlen(buf);
while (cp < bufend) {
if (*cp == '\n') {
/* Check for escaped newlines. */
if (cp > buf && *(cp - 1) == '\\')
*(cp - 1) = *cp = ' ';
else {
*cp = '\0';
lineno++;
conf_parse_line(trans, line, filename, lineno, section, subsection);
line = cp + 1;
}
}
cp++;
}
if (cp != line)
xlog_warn("conf_parse: last line non-terminated, ignored.");
}
static void
conf_load_defaults(void)
{
/* No defaults */
return;
}
static char *
conf_readfile(const char *path)
{
struct stat sb;
if (!path) {
xlog(L_ERROR, "conf_readfile: no path given");
return NULL;
}
if ((stat (path, &sb) == 0) || (errno != ENOENT)) {
char *new_conf_addr = NULL;
off_t sz;
int fd = open (path, O_RDONLY, 0);
if (fd == -1) {
xlog_warn("conf_readfile: open (\"%s\", O_RDONLY) failed", path);
return NULL;
}
/* Grab a shared lock to ensure its not mid-rewrite */
if (flock(fd, LOCK_SH)) {
xlog_warn("conf_readfile: attempt to grab read lock failed: %s",
strerror(errno));
goto fail;
}
/* only after we have the lock, check the file size ready to read it */
sz = lseek(fd, 0, SEEK_END);
if (sz < 0) {
xlog_warn("conf_readfile: unable to determine file size: %s",
strerror(errno));
goto fail;
}
lseek(fd, 0, SEEK_SET);
new_conf_addr = malloc(sz+1);
if (!new_conf_addr) {
xlog_warn("conf_readfile: malloc (%lu) failed", (unsigned long)sz);
goto fail;
}
/* XXX I assume short reads won't happen here. */
if (read (fd, new_conf_addr, sz) != (int)sz) {
xlog_warn("conf_readfile: read (%d, %p, %lu) failed",
fd, new_conf_addr, (unsigned long)sz);
goto fail;
}
close(fd);
/* XXX Should we not care about errors and rollback? */
new_conf_addr[sz] = '\0';
return new_conf_addr;
fail:
close(fd);
if (new_conf_addr)
free(new_conf_addr);
}
return NULL;
}
/* remove and free up any existing config state */
static void conf_free_bindings(void)
{
unsigned int i;
for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; i++) {
struct conf_binding *cb, *next;
cb = LIST_FIRST(&conf_bindings[i]);
for (; cb; cb = next) {
next = LIST_NEXT(cb, link);
LIST_REMOVE(cb, link);
free_confbind(cb);
}
LIST_INIT(&conf_bindings[i]);
}
}
static int
conf_load_files(int trans, const char *conf_file)
{
char *conf_data;
char *section = NULL;
char *subsection = NULL;
conf_data = conf_readfile(conf_file);
if (conf_data == NULL)
return 1;
/* Load default configuration values. */
conf_load_defaults();
/* Parse config contents into the transaction queue */
conf_parse(trans, conf_data, §ion, &subsection, conf_file);
if (section)
free(section);
if (subsection)
free(subsection);
free(conf_data);
return 0;
}
/* Open the config file and map it into our address space, then parse it. */
static int
conf_load_file(const char *conf_file)
{
int trans;
char * conf_data;
trans = conf_begin();
conf_data = conf_readfile(conf_file);
if (conf_data == NULL)
return 1;
/* Load default configuration values. */
conf_load_defaults();
/* Parse config contents into the transaction queue */
char *section = NULL;
char *subsection = NULL;
conf_parse(trans, conf_data, §ion, &subsection, conf_file);
if (section) free(section);
if (subsection) free(subsection);
free(conf_data);
/* Free potential existing configuration. */
conf_free_bindings();
/* Apply the new configuration values */
conf_end(trans, 1);
return 0;
}
static void
conf_init_dir(const char *conf_file)
{
struct dirent **namelist = NULL;
char *dname, fname[PATH_MAX], *cname;
int n = 0, nfiles = 0, i, fname_len, dname_len;
int trans, rv, path_len;
dname = malloc(strlen(conf_file) + 3);
if (dname == NULL) {
xlog(L_WARNING, "conf_init_dir: malloc: %s", strerror(errno));
return;
}
sprintf(dname, "%s.d", conf_file);
n = scandir(dname, &namelist, NULL, versionsort);
if (n < 0) {
if (errno != ENOENT) {
xlog(L_WARNING, "conf_init_dir: scandir %s: %s",
dname, strerror(errno));
}
free(dname);
return;
} else if (n == 0) {
free(dname);
return;
}
trans = conf_begin();
dname_len = strlen(dname);
for (i = 0; i < n; i++ ) {
struct dirent *d = namelist[i];
switch (d->d_type) {
case DT_UNKNOWN:
case DT_REG:
case DT_LNK:
break;
default:
continue;
}
if (*d->d_name == '.')
continue;
fname_len = strlen(d->d_name);
path_len = (fname_len + dname_len);
if (!fname_len || path_len > PATH_MAX) {
xlog(L_WARNING, "conf_init_dir: Too long file name: %s in %s",
d->d_name, dname);
continue;
}
/*
* Check the naming of the file. Only process files
* that end with CONF_FILE_EXT
*/
if (fname_len <= CONF_FILE_EXT_LEN) {
xlog(D_GENERAL, "conf_init_dir: %s: name too short",
d->d_name);
continue;
}
cname = (d->d_name + (fname_len - CONF_FILE_EXT_LEN));
if (strcmp(cname, CONF_FILE_EXT) != 0) {
xlog(D_GENERAL, "conf_init_dir: %s: invalid file extension",
d->d_name);
continue;
}
rv = snprintf(fname, PATH_MAX, "%s/%s", dname, d->d_name);
if (rv < path_len) {
xlog(L_WARNING, "conf_init_dir: file name: %s/%s too short",
d->d_name, dname);
continue;
}
if (conf_load_files(trans, fname))
continue;
nfiles++;
}
if (nfiles) {
/* Apply the configuration values */
conf_end(trans, 1);
}
for (i = 0; i < n; i++)
free(namelist[i]);
free(namelist);
free(dname);
return;
}
void
conf_init_file(const char *conf_file)
{
unsigned int i;
int j;
for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; i++)
LIST_INIT (&conf_bindings[i]);
TAILQ_INIT (&conf_trans_queue);
if (conf_file == NULL)
conf_file = NFS_CONFFILE;
/* If the config file is in /etc (normal) then check
* /usr/etc first. Also check config.conf.d for files
* names *.conf.
*
* Content or later files always over-rides earlier
* files.
*/
if (strncmp(conf_file, "/etc/", 5) == 0) {
char *usrconf = NULL;
j = asprintf(&usrconf, "/usr%s", conf_file);
if (usrconf && j > 0) {
conf_load_file(usrconf);
conf_init_dir(usrconf);
free(usrconf);
}
}
conf_load_file(conf_file);
conf_init_dir(conf_file);
}
/*
* Empty the config and free up any used memory
*/
void
conf_cleanup(void)
{
conf_free_bindings();
struct conf_trans *node, *next;
for (node = TAILQ_FIRST(&conf_trans_queue); node; node = next) {
next = TAILQ_NEXT(node, link);
TAILQ_REMOVE (&conf_trans_queue, node, link);
free_conftrans(node);
}
TAILQ_INIT(&conf_trans_queue);
}
/*
* Return the numeric value denoted by TAG in section SECTION or DEF
* if that tag does not exist.
*/
int
conf_get_num(const char *section, const char *tag, int def)
{
char *value = conf_get_str(section, tag);
if (value)
return atoi(value);
return def;
}
/*
* Return the Boolean value denoted by TAG in section SECTION, or DEF
* if that tags does not exist.
* FALSE is returned for case-insensitive comparisons with 0, f, false, n, no, off
* TRUE is returned for 1, t, true, y, yes, on
* A failure to match one of these results in DEF
*/
_Bool
conf_get_bool(const char *section, const char *tag, _Bool def)
{
char *value = conf_get_str(section, tag);
if (!value)
return def;
if (strcasecmp(value, "1") == 0 ||
strcasecmp(value, "t") == 0 ||
strcasecmp(value, "true") == 0 ||
strcasecmp(value, "y") == 0 ||
strcasecmp(value, "yes") == 0 ||
strcasecmp(value, "on") == 0)
return true;
if (strcasecmp(value, "0") == 0 ||
strcasecmp(value, "f") == 0 ||
strcasecmp(value, "false") == 0 ||
strcasecmp(value, "n") == 0 ||
strcasecmp(value, "no") == 0 ||
strcasecmp(value, "off") == 0)
return false;
return def;
}
/* Validate X according to the range denoted by TAG in section SECTION. */
int
conf_match_num(const char *section, const char *tag, int x)
{
char *value = conf_get_str (section, tag);
int val, min, max, n;
if (!value)
return 0;
n = sscanf (value, "%d,%d:%d", &val, &min, &max);
switch (n) {
case 1:
xlog(LOG_INFO, "conf_match_num: %s:%s %d==%d?", section, tag, val, x);
return x == val;
case 3:
xlog(LOG_INFO, "conf_match_num: %s:%s %d<=%d<=%d?", section,
tag, min, x, max);
return min <= x && max >= x;
default:
xlog(LOG_INFO, "conf_match_num: section %s tag %s: invalid number spec %s",
section, tag, value);
}
return 0;
}
/* Return the string value denoted by TAG in section SECTION. */
char *
conf_get_str(const char *section, const char *tag)
{
return conf_get_section(section, NULL, tag);
}
/* Return the string value denoted by TAG in section SECTION,
* unless it is not set, in which case return def
*/
char *
conf_get_str_with_def(const char *section, const char *tag, char *def)
{
char * result = conf_get_section(section, NULL, tag);
if (!result)
return def;
return result;
}
/*
* Retrieve an entry without interpreting its contents
*/
char *
conf_get_entry(const char *section, const char *arg, const char *tag)
{
struct conf_binding *cb;
cb = LIST_FIRST (&conf_bindings[conf_hash (section)]);
for (; cb; cb = LIST_NEXT (cb, link)) {
if (strcasecmp(section, cb->section) != 0)
continue;
if (arg && (cb->arg == NULL || strcasecmp(arg, cb->arg) != 0))
continue;
if (!arg && cb->arg)
continue;
if (strcasecmp(tag, cb->tag) != 0)
continue;
return cb->value;
}
return 0;
}
/*
* Find a section that may or may not have an argument
*/
char *
conf_get_section(const char *section, const char *arg, const char *tag)
{
struct conf_binding *cb;
retry:
cb = LIST_FIRST (&conf_bindings[conf_hash (section)]);
for (; cb; cb = LIST_NEXT (cb, link)) {
if (strcasecmp(section, cb->section) != 0)
continue;
if (arg && (cb->arg == NULL || strcasecmp(arg, cb->arg) != 0))
continue;
if (!arg && cb->arg)
continue;
if (strcasecmp(tag, cb->tag) != 0)
continue;
if (cb->value[0] == '$') {
/* expand $name from [environment] section,
* or from environment
*/
char *env = getenv(cb->value+1);
if (env && *env)
return env;
section = "environment";
tag = cb->value + 1;
goto retry;
}
return cb->value;
}
return 0;
}
/*
* Build a list of string values out of the comma separated value denoted by
* TAG in SECTION.
*/
struct conf_list *
conf_get_list(const char *section, const char *tag)
{
char *liststr = 0, *p, *field, *t;
struct conf_list *list = 0;
struct conf_list_node *node;
list = malloc (sizeof *list);
if (!list)
goto cleanup;
TAILQ_INIT (&list->fields);
list->cnt = 0;
liststr = conf_get_str(section, tag);
if (!liststr)
goto cleanup;
liststr = strdup (liststr);
if (!liststr)
goto cleanup;
p = liststr;
while ((field = strsep (&p, ",")) != NULL) {
/* Skip leading whitespace */
while (isspace (*field))
field++;
/* Skip trailing whitespace */
if (p) {
for (t = p - 1; t > field && isspace (*t); t--)
*t = '\0';
}
if (*field == '\0') {
xlog(LOG_INFO, "conf_get_list: empty field, ignoring...");
continue;
}
list->cnt++;
node = calloc (1, sizeof *node);
if (!node)
goto cleanup;
node->field = strdup (field);
if (!node->field) {
free(node);
goto cleanup;
}
TAILQ_INSERT_TAIL (&list->fields, node, link);
}
free (liststr);
return list;
cleanup:
if (list)
conf_free_list(list);
if (liststr)
free(liststr);
return 0;
}
struct conf_list *
conf_get_tag_list(const char *section, const char *arg)
{
struct conf_list *list = 0;
struct conf_list_node *node;
struct conf_binding *cb;
list = malloc(sizeof *list);
if (!list)
goto cleanup;
TAILQ_INIT(&list->fields);
list->cnt = 0;
cb = LIST_FIRST(&conf_bindings[conf_hash (section)]);
for (; cb; cb = LIST_NEXT(cb, link)) {
if (strcasecmp (section, cb->section) == 0) {
if (arg != NULL && strcasecmp(arg, cb->arg) != 0)
continue;
list->cnt++;
node = calloc(1, sizeof *node);
if (!node)
goto cleanup;
node->field = strdup(cb->tag);
if (!node->field) {
free(node);
goto cleanup;
}
TAILQ_INSERT_TAIL(&list->fields, node, link);
}
}
return list;
cleanup:
if (list)
conf_free_list(list);
return 0;
}
/* Decode a PEM encoded buffer. */
int
conf_decode_base64 (uint8_t *out, uint32_t *len, const unsigned char *buf)
{
uint32_t c = 0;
uint8_t c1, c2, c3, c4;
while (*buf) {
if (*buf > 127 || (c1 = asc2bin[*buf]) == 255)
return 0;
buf++;
if (*buf > 127 || (c2 = asc2bin[*buf]) == 255)
return 0;
buf++;
if (*buf == '=') {
c3 = c4 = 0;
c++;
/* Check last four bit */
if (c2 & 0xF)
return 0;
if (strcmp((char *)buf, "==") == 0)
buf++;
else
return 0;
} else if (*buf > 127 || (c3 = asc2bin[*buf]) == 255)
return 0;
else {
if (*++buf == '=') {
c4 = 0;
c += 2;
/* Check last two bit */
if (c3 & 3)
return 0;
if (strcmp((char *)buf, "="))
return 0;
} else if (*buf > 127 || (c4 = asc2bin[*buf]) == 255)
return 0;
else
c += 3;
}
buf++;
*out++ = (c1 << 2) | (c2 >> 4);
*out++ = (c2 << 4) | (c3 >> 2);
*out++ = (c3 << 6) | c4;
}
*len = c;
return 1;
}
void
conf_free_list(struct conf_list *list)
{
struct conf_list_node *node = TAILQ_FIRST(&list->fields);
while (node) {
TAILQ_REMOVE(&list->fields, node, link);
if (node->field)
free(node->field);
free (node);
node = TAILQ_FIRST(&list->fields);
}
free (list);
}
int
conf_begin(void)
{
static int seq = 0;
return ++seq;
}
static struct conf_trans *
conf_trans_node(int transaction, enum conf_op op)
{
struct conf_trans *node;
node = calloc (1, sizeof *node);
if (!node) {
xlog_warn("conf_trans_node: calloc (1, %lu) failed",
(unsigned long)sizeof *node);
return 0;
}
node->trans = transaction;
node->op = op;
TAILQ_INSERT_TAIL (&conf_trans_queue, node, link);
return node;
}
/* Queue a set operation. */
static int
conf_set(int transaction, const char *section, const char *arg,
const char *tag, const char *value, int override, int is_default)
{
struct conf_trans *node;
if (!value || !*value)
return 0;
node = conf_trans_node(transaction, CONF_SET);
if (!node)
return 1;
node->section = strdup(section);
if (!node->section) {
xlog_warn("conf_set: strdup(\"%s\") failed", section);
goto fail;
}
/* Make Section names case-insensitive */
upper2lower(node->section);
if (arg) {
node->arg = strdup(arg);
if (!node->arg) {
xlog_warn("conf_set: strdup(\"%s\") failed", arg);
goto fail;
}
} else
node->arg = NULL;
node->tag = strdup(tag);
if (!node->tag) {
xlog_warn("conf_set: strdup(\"%s\") failed", tag);
goto fail;
}
node->value = strdup(value);
if (!node->value) {
xlog_warn("conf_set: strdup(\"%s\") failed", value);
goto fail;
}
node->override = override;
node->is_default = is_default;
return 0;
fail:
free_conftrans(node);
return 1;
}
/* Queue a remove operation. */
int
conf_remove(int transaction, const char *section, const char *tag)
{
struct conf_trans *node;
node = conf_trans_node(transaction, CONF_REMOVE);
if (!node)
goto fail;
node->section = strdup(section);
if (!node->section) {
xlog_warn("conf_remove: strdup(\"%s\") failed", section);
goto fail;
}
node->tag = strdup(tag);
if (!node->tag) {
xlog_warn("conf_remove: strdup(\"%s\") failed", tag);
goto fail;
}
return 0;
fail:
free_conftrans(node);
return 1;
}
/* Queue a remove section operation. */
int
conf_remove_section(int transaction, const char *section)
{
struct conf_trans *node;
node = conf_trans_node(transaction, CONF_REMOVE_SECTION);
if (!node)
goto fail;
node->section = strdup(section);
if (!node->section) {
xlog_warn("conf_remove_section: strdup(\"%s\") failed", section);
goto fail;
}
return 0;
fail:
free_conftrans(node);
return 1;
}
/* Execute all queued operations for this transaction. Cleanup. */
int
conf_end(int transaction, int commit)
{
struct conf_trans *node, *next;
for (node = TAILQ_FIRST(&conf_trans_queue); node; node = next) {
next = TAILQ_NEXT(node, link);
if (node->trans == transaction) {
if (commit) {
switch (node->op) {
case CONF_SET:
conf_set_now(node->section, node->arg,
node->tag, node->value, node->override,
node->is_default);
break;
case CONF_REMOVE:
conf_remove_now(node->section, node->arg, node->tag);
break;
case CONF_REMOVE_SECTION:
conf_remove_section_now(node->section);
break;
default:
xlog(LOG_INFO, "conf_end: unknown operation: %d", node->op);
}
}
TAILQ_REMOVE (&conf_trans_queue, node, link);
free_conftrans(node);
}
}
return 0;
}
/*
* Dump running configuration upon SIGUSR1.
* Configuration is "stored in reverse order", so reverse it again.
*/
struct dumper {
char *section;
char *arg;
char *tag;
char *value;
struct dumper *next;
};
/*
* Test if two nodes belong to the same (sub)sections
*/
static int
dumper_section_compare(const struct dumper *nodea, const struct dumper *nodeb)
{
int ret;
/* missing node, shouldnt happen */
if (!nodea || !nodeb)
return -1;
/* no section names at all, they are equal */
if (!nodea->section && !nodeb->section)
return 0;
/* if only one has a section name, the blank one goes first */
if (!nodea->section && nodeb->section)
return -1;
if (nodea->section && !nodeb->section)
return 1;
/* both have section names, but do they match */
ret = strcmp(nodea->section, nodeb->section);
/* section names differ, that was easy */
if (ret != 0)
return ret;
/* sections matched, but how about sub-sections,
* again, if only one has a value the blank goes first
*/
if (!nodea->arg && nodeb->arg)
return -1;
if (nodea->arg && !nodeb->arg)
return 1;
/* both have sub-section args and they differ */
if (nodea->arg && nodeb->arg
&& (ret=strcmp(nodea->arg, nodeb->arg))!=0)
return ret;
return 0;
}
/* If a string starts or ends with a space it should be quoted */
static bool
should_escape(const char *text)
{
int len;
/* no string, no escaping needed */
if (!text)
return false;
/* first character is a space */
if (isspace(text[0]))
return true;
/* last character is a space */
len = strlen(text);
if (isspace(text[len-1]))
return true;
return false;
}
static void
conf_report_dump_text(struct dumper *head, FILE *ff)
{
const struct dumper *node = head;
const struct dumper *last = NULL;
for (node=head; node!=NULL; node=node->next) {
/* starting a new section, print the section header */
if (dumper_section_compare(last, node)!=0) {
if (node != head)
fprintf(ff, "\n");
if (node->arg)
fprintf(ff, "[%s \"%s\"]\n", node->section, node->arg);
else
fprintf(ff, "[%s]\n", node->section);
}
/* now print the tag and its value */
fprintf(ff, " %s", node->tag);
if (node->value) {
if (should_escape(node->value))
fprintf(ff, " = \"%s\"", node->value);
else
fprintf(ff, " = %s", node->value);
}
fprintf(ff, "\n");
last = node;
}
}
/* sort by tag compare function */
static int
dumper_compare(const void *a, const void *b)
{
const struct dumper *nodea = *(struct dumper **)a;
const struct dumper *nodeb = *(struct dumper **)b;
int ret;
/* missing node, shouldnt happen */
if (!nodea || !nodeb)
return -1;
/* are the two nodes in different (sub)sections */
ret = dumper_section_compare(nodea, nodeb);
if (ret != 0)
return ret;
/* sub-sections match (both blank, or both same)
* so we compare the tag names
*/
/* blank tags shouldnt happen, but paranoia */
if (!nodea->tag && !nodeb->tag)
return 0;
/* still shouldnt happen, but use the blank-goes-first logic */
if (!nodea->tag && nodeb->tag)
return -1;
if ( nodea->tag && !nodeb->tag)
return 1;
/* last test, compare the tags directly */
ret = strcmp(nodea->tag, nodeb->tag);
return ret;
}
/* sort all of the report nodes */
static struct dumper *
conf_report_sort(struct dumper *start)
{
struct dumper **list;
struct dumper *node;
unsigned int count = 0;
unsigned int i=0;
/* how long is this list */
for (node=start; node!=NULL; node=node->next)
count++;
/* no need to sort a list with less than 2 items */
if (count < 2)
return start;
/* build an array of all the nodes */
list = calloc(count, sizeof(struct dumper *));
if (!list)
goto mem_err;
for (node=start,i=0; node!=NULL; node=node->next) {
list[i++] = node;
}
/* sort the array alphabetically by section and tag */
qsort(list, count, sizeof(struct dumper *), dumper_compare);
/* rebuild the linked list in sorted order */
for (i=0; i<count-1; i++) {
list[i]->next = list[i+1];
}
list[count-1]->next = NULL;
/* remember the new head of list and discard the sorting array */
node = list[0];
free(list);
/* return the new head of list */
return node;
mem_err:
free(list);
return NULL;
}
/* Output a copy of the current configuration to file */
void
conf_report(FILE *outfile)
{
struct conf_binding *cb = NULL;
unsigned int i;
struct dumper *dumper = NULL, *dnode = NULL;
xlog(LOG_INFO, "conf_report: dumping running configuration");
/* build a linked list of all the config nodes */
for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; i++) {
for (cb = LIST_FIRST(&conf_bindings[i]); cb; cb = LIST_NEXT(cb, link)) {
struct dumper *newnode = calloc(1, sizeof (struct dumper));
if (!newnode)
goto mem_fail;
newnode->next = dumper;
dumper = newnode;
newnode->section = cb->section;
newnode->arg = cb->arg;
newnode->tag = cb->tag;
newnode->value = cb->value;
}
}
/* sort the list then print it */
dumper = conf_report_sort(dumper);
conf_report_dump_text(dumper, outfile);
goto cleanup;
mem_fail:
xlog_warn("conf_report: malloc/calloc failed");
cleanup:
/* traverse the linked list freeing all the nodes */
while ((dnode = dumper) != 0) {
dumper = dumper->next;
free(dnode);
}
return;
}
/* struct and queue for buffing output lines */
TAILQ_HEAD(tailhead, outbuffer);
struct outbuffer {
TAILQ_ENTRY(outbuffer) link;
char *text;
};
static struct outbuffer *
make_outbuffer(char *line)
{
struct outbuffer *new;
if (line == NULL)
return NULL;
new = calloc(1, sizeof(struct outbuffer));
if (new == NULL) {
xlog(L_ERROR, "malloc error creating outbuffer");
return NULL;
}
new->text = line;
return new;
}
/* compose a properly escaped tag=value line */
static char *
make_tagline(const char *tag, const char *value)
{
char *line;
int ret;
if (!value)
return NULL;
if (should_escape(value))
ret = asprintf(&line, "%s = \"%s\"\n", tag, value);
else
ret = asprintf(&line, "%s = %s\n", tag, value);
if (ret == -1) {
xlog(L_ERROR, "malloc error composing a tag line");
return NULL;
}
return line;
}
/* compose a section header line */
static char *
make_section(const char *section, const char *arg)
{
char *line;
int ret;
if (arg)
ret = asprintf(&line, "[%s \"%s\"]\n", section, arg);
else
ret = asprintf(&line, "[%s]\n", section);
if (ret == -1) {
xlog(L_ERROR, "malloc error composing section header");
return NULL;
}
return line;
}
/* compose a comment line (with or without tag) */
static char *
make_comment(const char *tag, const char *comment)
{
char *line;
int ret;
if (tag == NULL || *tag == '\0') {
ret = asprintf(&line, "# %s\n", comment);
} else {
ret = asprintf(&line, "# %s: %s\n", tag, comment);
}
if (ret == -1) {
xlog(L_ERROR, "malloc error composing header");
return NULL;
}
return line;
}
/* compose a 'file modified' comment */
static char *
make_timestamp(const char *tag, time_t when)
{
struct tm *tstamp;
char datestr[80];
char *result = NULL, *tmpstr = NULL;
int ret;
tstamp = localtime(&when);
if (strftime(datestr, sizeof(datestr), "%b %d %Y %H:%M:%S", tstamp) == 0) {
xlog(L_ERROR, "error composing date");
datestr[0] = '\0';
}
if (modified_by) {
ret = asprintf(&tmpstr, "%s on %s", modified_by, datestr);
if (ret == -1) {
xlog(L_ERROR, "malloc error composing a time stamp");
return NULL;
}
result = make_comment(tag, tmpstr);
free(tmpstr);
} else {
result = make_comment(tag, datestr);
}
return result;
}
/* does the supplied line contain the named section header */
static bool
is_section(const char *line, const char *section, const char *arg)
{
char *end;
char *name;
char *sub;
bool found = false;
/* Not a valid section name */
if (strcmp(section, "#") == 0)
return false;
/* skip leading white space */
while (*line == '[' || isspace(*line))
line++;
name = strdup(line);
if (name == NULL) {
xlog_warn("conf_write: malloc failed ");
return false;
}
/* find the end */
end = strchr(name, ']');
/* malformed line */
if (end == NULL) {
xlog_warn("conf_write: warning: malformed section name");
goto cleanup;
}
while (*end && ( *end == ']' || isblank(*end)))
*(end--) = '\0';
/* is there a subsection name (aka arg) */
sub = strchr(name, '"');
if (sub) {
end = sub - 1;
*(sub++) = '\0';
/* trim whitespace between section name and arg */
while (end > name && isblank(*end))
*(end--) = '\0';
/* trim off closing quote */
end = strchr(sub, '"');
if (end == NULL) {
xlog_warn("conf_write: warning: malformed sub-section name");
goto cleanup;
}
*end = '\0';
}
/* ready to compare */
if (strcasecmp(section, name)!=0)
goto cleanup;
if (arg != NULL) {
if (sub == NULL || strcasecmp(arg, sub)!=0)
goto cleanup;
} else {
if (sub != NULL)
goto cleanup;
}
found = true;
cleanup:
free(name);
return found;
}
/* check that line contains the specified tag assignment */
static bool
is_tag(const char *line, const char *tagname)
{
char *end;
char *name;
bool found = false;
/* quick check, is this even an assignment line */
end = strchr(line, '=');
if (end == NULL)
return false;
/* skip leading white space before tag name */
while (isblank(*line))
line++;
name = strdup(line);
if (name == NULL) {
xlog_warn("conf_write: malloc failed");
return false;
}
/* trim any newline characters */
end = strchr(name, '\n');
if (end)
*end = '\0';
end = strchr(name, '\r');
if (end)
*end = '\0';
/* find the assignment equals sign */
end = strchr(name, '=');
/* malformed line, i swear the equals was there earlier */
if (end == NULL) {
xlog_warn("conf_write: warning: malformed tag name");
goto cleanup;
}
/* trim trailing whitespace after tag name */
do {
*(end--) = '\0';
}while (end > name && *end && isblank(*end));
/* quoted string, take contents of quotes only */
if (*name == '"') {
char * new = strdup(name+1);
end = strchr(new, '"');
if (end != NULL) {
*end = 0;
free(name);
name = new;
} else {
free(new);
}
}
/* now compare */
if (strcasecmp(tagname, name) == 0)
found = true;
cleanup:
free(name);
return found;
}
/* is this an empty line ? */
static bool
is_empty(const char *line)
{
const char *p = line;
if (line == NULL)
return true;
if (*line == '\0')
return true;
while (*p != '\0' && isspace(*p))
p++;
if (*p == '\0')
return true;
return false;
}
/* is this line just a comment ? */
static bool
is_comment(const char *line)
{
if (line == NULL)
return false;
while (isblank(*line))
line++;
if (*line == '#')
return true;
return false;
}
/* check that line contains the specified comment header */
static bool
is_taggedcomment(const char *line, const char *field)
{
char *end;
char *name;
bool found = false;
if (line == NULL)
return false;
while (isblank(*line))
line++;
if (*line != '#')
return false;
line++;
/* quick check, is this even a likely formatted line */
end = strchr(line, ':');
if (end == NULL)
return false;
/* skip leading white space before field name */
while (isblank(*line))
line++;
name = strdup(line);
if (name == NULL) {
xlog_warn("conf_write: malloc failed");
return false;
}
/* strip trailing spaces from the name */
end = strchr(name, ':');
if (end) *(end--) = 0;
while (end && end > name && isblank(*end))
*(end--)=0;
if (strcasecmp(name, field)==0)
found = true;
free(name);
return found;
}
/* delete a buffer queue whilst optionally outputting to file */
static int
flush_outqueue(struct tailhead *queue, FILE *fout)
{
int ret = 0;
while (queue->tqh_first != NULL) {
struct outbuffer *ob = queue->tqh_first;
TAILQ_REMOVE(queue, ob, link);
if (ob->text) {
if (fout) {
ret = fprintf(fout, "%s", ob->text);
if (ret == -1) {
xlog(L_ERROR, "Error writing to config file: %s",
strerror(errno));
fout = NULL;
}
}
free(ob->text);
}
free(ob);
}
if (ret == -1)
return 1;
return 0;
}
/* append one queue to another */
static void
append_queue(struct tailhead *inq, struct tailhead *outq)
{
while (inq->tqh_first != NULL) {
struct outbuffer *ob = inq->tqh_first;
TAILQ_REMOVE(inq, ob, link);
TAILQ_INSERT_TAIL(outq, ob, link);
}
}
/* read one line of text from a file, growing the buffer as necessary */
static int
read_line(char **buff, int *buffsize, FILE *in)
{
char *readp;
int used = 0;
bool again = false;
/* make sure we have a buffer to read into */
if (*buff == NULL) {
*buffsize = 4096;
*buff = calloc(1, *buffsize);
if (*buff == NULL) {
xlog(L_ERROR, "malloc error for read buffer");
return -1;
}
}
readp = *buff;
do {
int len;
/* read in a chunk */
if (fgets(readp, *buffsize-used, in)==NULL)
return -1;
len = strlen(*buff);
if (len == 0)
return -1;
/* was this the end of a line, or partial read */
readp = *buff + len - 1;
if (*readp != '\n' && *readp !='\r') {
/* no nl/cr must be partial read, go again */
readp++;
again = true;
} else {
/* that was a normal end of line */
again = false;
}
/* do we need more space */
if (again && *buffsize - len < 1024) {
int offset = readp - *buff;
char *newbuff;
*buffsize += 4096;
newbuff = realloc(*buff, *buffsize);
if (newbuff == NULL) {
xlog(L_ERROR, "malloc error reading line");
return -1;
}
*buff = newbuff;
readp = newbuff + offset;
}
} while(again);
return 0;
}
/* append a line to the given location in the queue */
static int
append_line(struct tailhead *queue, struct outbuffer *entry, char *line)
{
int ret = 0;
char *end;
bool splitmode = false;
char *start = line;
if (line == NULL)
return -1;
/* if there are \n's in the middle of the string
* then we need to split it into folded lines */
do {
char *thisline;
struct outbuffer *qbuff;
end = strchr(start, '\n');
if (end && *(end+1) != '\0') {
*end = '\0';
ret = asprintf(&thisline, "%s\\\n", start);
if (ret == -1) {
xlog(L_ERROR, "malloc error composing output");
return -1;
}
splitmode = true;
start = end+1;
} else {
end = NULL;
if (splitmode) {
thisline = strdup(start);
if (thisline == NULL)
return -1;
} else {
thisline = start;
}
}
qbuff = make_outbuffer(thisline);
if (qbuff == NULL)
return -1;
if (entry) {
TAILQ_INSERT_AFTER(queue, entry, qbuff, link);
entry = TAILQ_NEXT(entry, link);
} else {
TAILQ_INSERT_TAIL(queue, qbuff, link);
}
}while (end != NULL);
/* we malloced copies of this, so free the original */
if (splitmode)
free(line);
return 0;
}
/* is this a "folded" line, i.e. ends in backslash */
static bool
is_folded(const char *line)
{
const char *end;
if (line == NULL)
return false;
end = line + strlen(line);
while (end > line) {
end--;
if (*end != '\n' && *end != '\r')
break;
}
if (*end == '\\')
return true;
return false;
}
static int
lock_file(FILE *f)
{
int ret;
ret = flock(fileno(f), LOCK_EX);
if (ret)
xlog(L_ERROR, "Error could not lock the file");
return ret;
}
/***
* Write a value to an nfs.conf style filename
*
* create the file if it doesnt already exist
* if value==NULL removes the setting (if present)
*/
int
conf_write(const char *filename, const char *section, const char *arg,
const char *tag, const char *value)
{
FILE *infile = NULL;
int ret = 1;
struct tailhead outqueue;
struct tailhead inqueue;
char * buff = NULL;
int buffsize = 0;
time_t now = time(NULL);
TAILQ_INIT(&inqueue);
TAILQ_INIT(&outqueue);
if (!filename) {
xlog_warn("conf_write: no filename supplied");
return ret;
}
if (!section || !tag) {
xlog_warn("conf_write: section or tag name missing");
return ret;
}
infile = fopen(filename, "r+");
if (!infile) {
if (!value) {
xlog_warn("conf_write: config file \"%s\" not found, nothing to do", filename);
ret = 0;
goto cleanup;
}
xlog_warn("conf_write: config file \"%s\" not found, creating.", filename);
infile = fopen(filename, "wx");
if (!infile) {
xlog(L_ERROR, "conf_write: Error creating config file \"%s\".", filename);
goto cleanup;
}
if (lock_file(infile))
goto cleanup;
if (strcmp(section, "#") == 0) {
if (append_line(&inqueue, NULL, make_comment(tag, value)))
goto cleanup;
} else {
if (append_line(&inqueue, NULL, make_section(section, arg)))
goto cleanup;
if (append_line(&inqueue, NULL, make_tagline(tag, value)))
goto cleanup;
}
append_queue(&inqueue, &outqueue);
} else
if (strcmp(section, "#") == 0) {
/* Adding a comment line */
struct outbuffer *where = NULL;
struct outbuffer *next = NULL;
bool found = false;
int err = 0;
if (lock_file(infile))
goto cleanup;
buffsize = 4096;
buff = calloc(1, buffsize);
if (buff == NULL) {
xlog(L_ERROR, "malloc error for read buffer");
goto cleanup;
}
buff[0] = '\0';
/* read in the file */
do {
if (*buff != '\0'
&& !is_taggedcomment(buff, "Modified")) {
if (append_line(&inqueue, NULL, strdup(buff)))
goto cleanup;
}
err = read_line(&buff, &buffsize, infile);
} while (err == 0);
/* if a tagged comment, look for an existing instance */
if (tag && *tag != '\0') {
where = TAILQ_FIRST(&inqueue);
while (where != NULL) {
next = TAILQ_NEXT(where, link);
struct outbuffer *prev = TAILQ_PREV(where, tailhead, link);
if (is_taggedcomment(where->text, tag)) {
TAILQ_REMOVE(&inqueue, where, link);
free(where->text);
free(where);
found = true;
if (append_line(&inqueue, prev, make_comment(tag, value)))
goto cleanup;
}
where = next;
}
}
/* it wasn't tagged or we didn't find it */
if (!found) {
/* does the file end in a blank line or a comment */
if (!TAILQ_EMPTY(&inqueue)) {
struct outbuffer *tail = TAILQ_LAST(&inqueue, tailhead);
if (tail && !is_empty(tail->text) && !is_comment(tail->text)) {
/* no, so add one for clarity */
if (append_line(&inqueue, NULL, strdup("\n")))
goto cleanup;
}
}
/* add the new comment line */
if (append_line(&inqueue, NULL, make_comment(tag, value)))
goto cleanup;
}
/* move everything over to the outqueue for writing */
append_queue(&inqueue, &outqueue);
} else {
bool found = false;
int err = 0;
if (lock_file(infile))
goto cleanup;
buffsize = 4096;
buff = calloc(1, buffsize);
if (buff == NULL) {
xlog(L_ERROR, "malloc error for read buffer");
goto cleanup;
}
buff[0] = '\0';
do {
struct outbuffer *where = NULL;
/* read in one section worth of lines */
do {
if (*buff != '\0'
&& !is_taggedcomment(buff, "Modified")) {
if (append_line(&inqueue, NULL, strdup(buff)))
goto cleanup;
}
err = read_line(&buff, &buffsize, infile);
} while (err == 0 && buff[0] != '[');
/* find the section header */
where = TAILQ_FIRST(&inqueue);
while (where != NULL) {
if (where->text != NULL && where->text[0] == '[')
break;
where = TAILQ_NEXT(where, link);
}
/* this is the section we care about */
if (where != NULL && is_section(where->text, section, arg)) {
struct outbuffer *section_start = where;
/* is there an existing assignment */
while ((where = TAILQ_NEXT(where, link)) != NULL) {
if (is_tag(where->text, tag)) {
found = true;
break;
}
}
/* no active assignment, but is there a commented one */
if (!found) {
where = section_start;
while ((where = TAILQ_NEXT(where, link)) != NULL) {
if (is_comment(where->text)) {
char *cline = where->text;
while (isspace(*cline))
cline++;
if (*cline != '#')
continue;
cline++;
if (is_tag(cline, tag)) {
found = true;
break;
}
}
}
}
/* replace the located tag with an updated one */
if (found) {
struct outbuffer *prev = TAILQ_PREV(where, tailhead, link);
bool again = false;
/* remove current tag */
do {
struct outbuffer *next = TAILQ_NEXT(where, link);
TAILQ_REMOVE(&inqueue, where, link);
if (is_folded(where->text))
again = true;
else
again = false;
free(where->text);
free(where);
where = next;
} while(again && where != NULL);
/* insert new tag */
if (value) {
if (append_line(&inqueue, prev, make_tagline(tag, value)))
goto cleanup;
}
} else
/* no existing assignment found and we need to add one */
if (value) {
/* rewind past blank lines and comments */
struct outbuffer *tail = TAILQ_LAST(&inqueue, tailhead);
/* comments immediately before a section usually relate
* to the section below them */
while (tail != NULL && is_comment(tail->text))
tail = TAILQ_PREV(tail, tailhead, link);
/* there is usually blank line(s) between sections */
while (tail != NULL && is_empty(tail->text))
tail = TAILQ_PREV(tail, tailhead, link);
/* now add the tag here */
if (append_line(&inqueue, tail, make_tagline(tag, value)))
goto cleanup;
found = true;
}
}
/* EOF and correct section not found, so add one */
if (err && !found && value) {
/* did the last section end in a blank line */
struct outbuffer *tail = TAILQ_LAST(&inqueue, tailhead);
if (tail && !is_empty(tail->text)) {
/* no, so add one for clarity */
if (append_line(&inqueue, NULL, strdup("\n")))
goto cleanup;
}
/* add the new section header */
if (append_line(&inqueue, NULL, make_section(section, arg)))
goto cleanup;
/* now add the tag */
if (append_line(&inqueue, NULL, make_tagline(tag, value)))
goto cleanup;
}
/* we are done with this section, move it to the out queue */
append_queue(&inqueue, &outqueue);
} while(err == 0);
}
if (modified_by) {
/* check for and update the Modified header */
/* does the file end in a blank line or a comment */
if (!TAILQ_EMPTY(&outqueue)) {
struct outbuffer *tail = TAILQ_LAST(&outqueue, tailhead);
if (tail && !is_empty(tail->text) && !is_comment(tail->text)) {
/* no, so add one for clarity */
if (append_line(&outqueue, NULL, strdup("\n")))
goto cleanup;
}
}
/* now append the modified date comment */
if (append_line(&outqueue, NULL, make_timestamp("Modified", now)))
goto cleanup;
}
/* now rewind and overwrite the file with the updated data */
rewind(infile);
if (ftruncate(fileno(infile), 0)) {
xlog(L_ERROR, "Error truncating config file");
goto cleanup;
}
if (flush_outqueue(&outqueue, infile))
goto cleanup;
if (infile) {
fclose(infile);
infile = NULL;
}
ret = 0;
cleanup:
flush_outqueue(&inqueue, NULL);
flush_outqueue(&outqueue, NULL);
if (buff)
free(buff);
if (infile)
fclose(infile);
return ret;
}
|