Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 1927 lines (1607 sloc) 55.177 kb
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
/*********************************************************************
*
* Copyright 2010 by Sean Conner. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Comments, questions and criticisms can be sent to: sean@conman.org
*
*********************************************************************/

/*******************************************************************
*
* SyslogInterpreter - a syslog replacement.
*
* This binds to the various syslog sockets, collects syslog requests, and
* then passes them on to a Lua function for handling. The C code will
* construct a Lua table with the following fields:
*
* version integer = 0
* facility string
* level string
* timestamp as from os.time()
* logtimestamp as from os.time() [1]
* pid integer (0 if not available) [2]
* program string ("" if not available) [3]
* msg actual string
* remote boolean
* host string [4]
* relay string ("" if not available) [6]
* port integer (-1 if not available) [5]
* localaddr string [4][7]
* localport integer (-1 if not available) [7]
*
* and then pass it to a Lua function called log(). That function can then
* do whatever it wants with the information.
*
* Sending this program a SIGUSR1 will cause it to reload the given script,
* meaning you can extend the script, then have the changes take affect
* without restarting the program.
*
* Sending this program a SIGHUP will cause it to look for a Lua function
* called reload_signal() and call that. reload_signal() has no parameters,
* and returns no parameters.
*
* You can also schedule a function to run periodically with the Lua function
* alarm(n). The parameter is either a number, which is the number of
* seconds between invocations of the supplied alarm_handler() function, or a
* string, which has a format of "<number>s" (for number of seconds),
* "<number>m" (for number of minutes), "<number>h" (for number of hours) or
* "<number>d" (for number of days). The function alarm_handler() takes no
* parameters, nor returns any paramters.
*
* There are also two Lua variables defined:
*
* scriptpath - the full path to the script being run
* script - the basename of the script being run
*
* To compile (assuming the Lua header files and library are installed):
*
* Linux:
* gcc -std=c99 -rdynamic -g -o syslogintr syslogintr.c -ldl -lm -llua
*
* Mac-OS/X
* gcc -std=c99 -g -o syslogintr syslogintr.c -ldl -lm -llua
*
* OpenBSD
* gcc -std=c99 -rdynamic -g -o syslogintr syslogintr.c -lm -llua
*
* Solaris (using native Sun compiler)
* cc -xc99 -g -o syslogintr syslogintr.c -llua -ldl -lm -lsocket -lnsl
*
* [1] if the incoming syslog request has a timestamp, this will contain
* it, otherwise, it's equal to the timestamp field.
*
* [2] if the incoming syslog request has a PID field.
*
* [3] if the incoming syslog request has a program field.
*
* [4] IP address (IPv4 or IPv6) of the request. If it's from a local
* socket this will be the filename of the localsocket.
*
* [5] Remote port of the request, or 0 if from the localsocket.
*
* [6] The message is being relayed from an original source. If that
* is the case, then host will be set to the original source, and
* relay will be set to the device that sent us the message. If
* the device was the original sender, then relay will be "".
*
* [7] The information relates to the local socket. Useful if you
* have a large number of listening sockets and need to filter
* on that information.
************************************************************************/

#define _GNU_SOURCE
#define _POSIX_PTHREAD_SEMANTICS

#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdarg.h>

#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <poll.h>
#include <unistd.h>
#include <fcntl.h>

#include <syslog.h>
#include <signal.h>
#include <pwd.h>
#include <grp.h>
#include <getopt.h>

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

#define VERSION "1.2.1"

#define MAX_FACILITY 24
#define MAX_LEVEL 8
#define MAX_SOCKETS 18
#define MAX_MSGLEN 1024

#define LOG_PORT 514
#define LOG_LOCAL "/dev/log"
#define LOG_IPv4 "0.0.0.0"
#define LOG_IPv6 "::"

#define LUA_CODE "/usr/local/sbin/syslog.lua"
#define LUA_UD_HOST "SOCKADDR"

#define PID_FILE "/var/run/syslogd.pid"
#define DEV_NULL "/dev/null"

/************************************************************************
*
* And now the ugly, non-portable bits. This stuff bites and I hate that
* I have to do this, but alas, if I want this to compile on stuff other
* than my own system ...
*
************************************************************************/

#ifdef __APPLE__
# include <libgen.h>
# undef LOG_LOCAL
# define LOG_LOCAL "/var/run/syslog"
# ifndef AI_NUMERICSERV
# define AI_NUMERICSERV 0
# endif
# ifndef _SC_GETPW_R_SIZE_MAX
# define sysconf(x) BUFSIZ
# endif
#endif

#ifdef __OpenBSD__
# include <libgen.h>
#endif

#ifdef __SunOS
# include <libgen.h>
# include <sys/un.h>
# define AF_LOCAL AF_UNIX
#endif

/*****************************************************************/

enum
{
  OPT_NONE = '\0',
  OPT_USER = 'u',
  OPT_GROUP = 'g',
  OPT_IPv4 = '4',
  OPT_IPv6 = '6',
  OPT_IPv4int = 'i',
  OPT_IPv6int = 'I',
  OPT_LOCAL = 'l',
  OPT_SOCKET = 's',
  OPT_FG = 'f',
  OPT_PATH = 'p',
  OPT_CPATH = 'c',
  OPT_HELP = 'h',
  OPT_VERSION = 'v',
  OPT_ERR = '?'
};

typedef struct status
{
  bool okay;
  int err;
  const char *msg;
} Status;

typedef union sockaddr_all
{
  struct sockaddr ss;
  struct sockaddr_in sin;
  struct sockaddr_in6 sin6;
  struct sockaddr_un ssun;
  struct sockaddr_storage ssto;
} sockaddr_all;

typedef struct socket_node
{
  int sock;
  socklen_t len;
  sockaddr_all local;
} *SocketNode;

struct sysstring
{
  size_t size;
  const char *text;
};

typedef struct node
{
  struct node *ln_Succ;
  struct node *ln_Pred;
} Node;

typedef struct list
{
  struct node *lh_Head;
  struct node *lh_Tail;
  struct node *lh_TailPred;
} List;

struct msg
{
  Node node; /* used to internally queue log msgs */
  int version; /* syslog version---RFC3164=0 */
  struct sysstring raw; /* raw message (debugging purposes) */
  struct sysstring host; /* address of original sending host */
  struct sysstring relay; /* address of host that sent msg */
  int port; /* UDP port of sending host */
  struct sysstring laddr; /* address of receiving host (us) */
  int lport; /* UDP port of receiving host (us) */
  bool remote; /* true if syslog from remote */
  time_t timestamp; /* timestamp of received syslog */
  time_t logtimestamp; /* original timestamp */
  struct sysstring program; /* program that generated syslog */
  int pid; /* process id of said program */
  int facility;
  int level;
  struct sysstring msg; /* syslog message */
};

/******************************************************************/

Status ipv4_socket (const char *);
Status ipv6_socket (const char *);
Status local_socket (const char *);
Status create_socket (SocketNode,socklen_t);
void event_read (SocketNode);
void syslog_interp (sockaddr_all *,sockaddr_all *,const char *,const char *);
void process_msg (const struct msg *const);
Status globalv_init (int,char *[]);
void usage (const char *);
Status drop_privs (void);
Status daemon_init (void);
void load_script (void);
int map_str_to_int (const char *,const char *const [],size_t);
void handle_signal (int);
Status set_signal_handler (int,void (*)(int));
int syslogintr_alarm (lua_State *);
int syslogintr_ud__toprint (lua_State *);
int syslogintr_host (lua_State *);
int syslogintr_lsock (lua_State *);
int syslogintr_relay (lua_State *);
void call_optional_luaf (const char *);
void internal_log (int,const char *,...);
Node *ListRemHead (List *const);
void NodeInsert (Node *const,Node *const);
void NodeRemove (Node *const);
void add_lua_path (const char *,const char *);

/******************************************************************/

static inline void ListInit(List *const pl)
{
  assert(pl != NULL);
  
  pl->lh_Head = (Node *)&pl->lh_Tail;
  pl->lh_Tail = NULL;
  pl->lh_TailPred = (Node *)&pl->lh_Head;
}

static inline void ListAddTail(List *const pl,Node *const pn)
{
  assert(pl != NULL);
  assert(pn != NULL);
  
  NodeInsert(pl->lh_TailPred,pn);
}

static inline Node *ListGetHead(List *const pl)
{
  assert(pl != NULL);
  assert(pl->lh_Head != NULL);
  
  return pl->lh_Head;
}

static inline bool NodeValid(Node *const pn)
{
  assert(pn != NULL);
  
  if (pn->ln_Succ == NULL) return false;
  if (pn->ln_Pred == NULL) return false;
  return true;
}

static inline Status retstatus(bool okay,int err,const char *msg)
{
  assert(msg != NULL);
  
  return (Status){
                   .okay = okay,
                   .err = err,
                   .msg = msg
                 };
}

/******************************************************************/

extern char *optarg;
extern int optind;
extern int opterr;
extern int optopt;

unsigned int g_alarm;
const char *g_luacode = LUA_CODE;
const char *g_user;
const char *g_group;
const char *g_lpath;
const char *g_lcpath;
bool gf_foreground;
lua_State *g_L;
struct socket_node g_sockets[MAX_SOCKETS];
size_t g_maxsocket;
size_t g_ipv4;
size_t g_ipv6;
struct sysstring g_logtag;
List g_intlog;

const struct option c_options [] =
{
  { "foreground" , no_argument , NULL , OPT_FG } ,
  { "version" , no_argument , NULL , OPT_VERSION } ,
  { "ip" , no_argument , NULL , OPT_IPv4 } ,
  { "ip4" , no_argument , NULL , OPT_IPv4 } ,
  { "ipv4" , no_argument , NULL , OPT_IPv4 } ,
  { "ipaddr" , required_argument , NULL , OPT_IPv4int } ,
  { "ip4addr" , required_argument , NULL , OPT_IPv4int } ,
  { "ipv4addr" , required_argument , NULL , OPT_IPv4int } ,
  { "ip6" , no_argument , NULL , OPT_IPv6 } ,
  { "ipv6" , no_argument , NULL , OPT_IPv6 } ,
  { "ip6addr" , required_argument , NULL , OPT_IPv6int } ,
  { "ipv6addr" , required_argument , NULL , OPT_IPv6int } ,
  { "local" , no_argument , NULL , OPT_LOCAL } ,
  { "socket" , required_argument , NULL , OPT_SOCKET } ,
  { "user" , required_argument , NULL , OPT_USER } ,
  { "group" , required_argument , NULL , OPT_GROUP } ,
  { "lua-path" , required_argument , NULL , OPT_PATH } ,
  { "lua-cpath" , required_argument , NULL , OPT_CPATH } ,
  { "help" , no_argument , NULL , OPT_HELP } ,
  { NULL , 0 , NULL , 0 }
};

const char *const c_facility[] =
{
  "kernel",
  "user",
  "mail",
  "daemon",
  "auth1",
  "syslog",
  "lpr",
  "news",
  "uucp",
  "cron1",
  "auth2", /* also authpriv */
  "ftp",
  "ntp",
  "auth3",
  "auth4",
  "cron2",
  "local0",
  "local1",
  "local2",
  "local3",
  "local4",
  "local5",
  "local6",
  "local7"
};

const char *const c_level[] =
{
  "emerg",
  "alert",
  "crit",
  "err",
  "warn",
  "notice",
  "info",
  "debug"
};

const struct sysstring c_null = { 0 , "" } ;
const struct status c_okay = { true , 0 , "" } ;

volatile sig_atomic_t mf_sigint;
volatile sig_atomic_t mf_sigusr1;
volatile sig_atomic_t mf_sighup;
volatile sig_atomic_t mf_sigalarm;

/***************************************************************/

int main(int argc,char *argv[])
{
  Status status;
    
  status = globalv_init(argc,argv);
  if (!status.okay)
  {
    if (status.err != 0)
      perror(status.msg);
    return EXIT_FAILURE;
  }

  if (!gf_foreground)
  {
    FILE *fppid;
    
    status = daemon_init();
    if (!status.okay)
    {
      perror(status.msg);
      return EXIT_FAILURE;
    }
    
    fppid = fopen(PID_FILE,"w");
    if (fppid)
    {
      fprintf(fppid,"%lu\n",(unsigned long)getpid());
      fclose(fppid);
    }
  }

  status = drop_privs();
  if (!status.okay)
  {
    perror(status.msg);
    return EXIT_FAILURE;
  }

  g_L = lua_open();
  if (g_L == NULL)
  {
    perror("lua_open()");
    return EXIT_FAILURE;
  }
  
  lua_gc(g_L,LUA_GCSTOP,0);
  luaL_openlibs(g_L);
  lua_gc(g_L,LUA_GCRESTART,0);

  lua_pushstring(g_L,g_luacode);
  lua_setglobal(g_L,"scriptpath");
  lua_pushstring(g_L,basename(g_luacode)); /* Solaris bitches about this line */
  lua_setglobal(g_L,"script");

  lua_register(g_L,"alarm",syslogintr_alarm);
  lua_register(g_L,"host", syslogintr_host);
  lua_register(g_L,"lsock",syslogintr_lsock);
  lua_register(g_L,"relay",syslogintr_relay);

  /*--------------------------------------------------
; this metatable exists so we can call tostring() on
; our "host" userdata.
;---------------------------------------------------*/
  
  luaL_newmetatable(g_L,LUA_UD_HOST);
  lua_pushcfunction(g_L,syslogintr_ud__toprint);
  lua_setfield(g_L,-2,"__tostring");
  lua_pop(g_L,1);
  
  if (g_lpath)
    add_lua_path("path",g_lpath);
  
  if (g_lcpath)
    add_lua_path("cpath",g_lcpath);

  set_signal_handler(SIGINT, handle_signal);
  set_signal_handler(SIGUSR1,handle_signal);
  set_signal_handler(SIGHUP ,handle_signal);
  set_signal_handler(SIGALRM,handle_signal);
  set_signal_handler(SIGTERM,handle_signal);
  
  load_script();
  internal_log(LOG_DEBUG,"PID: %lu",(unsigned long)getpid());
  
  struct pollfd events[g_maxsocket];
  
  for (size_t i = 0 ; i < g_maxsocket ; i++)
  {
    events[i].fd = g_sockets[i].sock;
    events[i].events = POLLIN;
  }

  while(!mf_sigint)
  {
    assert(lua_gettop(g_L) == 0);
    
    if (mf_sigusr1)
    {
      mf_sigusr1 = 0;
      load_script();
    }

    if (mf_sighup)
    {
      mf_sighup = 0;
      call_optional_luaf("reload_signal");
    }
    
    if (mf_sigalarm)
    {
      mf_sigalarm = 0;
      call_optional_luaf("alarm_handler");
    }
    
    /*--------------------------------------------------------------------
; check for any locally queued messages, and pass them all along. Don't
; forget to clean up afterwards.
;---------------------------------------------------------------------*/
    
    for (
          struct msg *msg = (struct msg *)ListRemHead(&g_intlog);
          NodeValid(&msg->node);
          msg = (struct msg *)ListRemHead(&g_intlog)
        )
    {
      process_msg(msg);
      assert(msg->raw.text == msg->msg.text);
      free((void *)msg->raw.text);
      free(msg);
    }
    
    /*---------------------------------------------------------------
; now check for any requests outside of ourselves
;-------------------------------------------------------------*/
    
    if (poll(events,g_maxsocket,-1) < 1)
      continue; /* continue on errors and timeouts */
    
    for (size_t i = 0 ; i < g_maxsocket; i++)
    {
      if ((events[i].revents & POLLIN))
        event_read(&g_sockets[i]);
    }
  }

  call_optional_luaf("cleanup");
  lua_close(g_L);
  
  for (size_t i = 0 ; i < g_maxsocket; i++)
  {
    if (g_sockets[i].sock != -1) close(g_sockets[i].sock);
    if (g_sockets[i].local.ss.sa_family == AF_LOCAL)
      unlink(g_sockets[i].local.ssun.sun_path);
  }
  
  if (!gf_foreground)
    unlink(PID_FILE); /* don't care if this succeeds or not */
  
  /*------------------------------------------------------------------------
; Per http://www.cons.org/cracauer/sigint.html, the only proper way to
; exit a program that has received a SIGINT (or SIGQUIT) is to actually
; use the default action of SIGINT (or SIGQUIT) so that the calling
; program can detect that the child process (in this case, us) actually
; received a signal. So that's why we set the default handler for SIGINT
; and kill ourselves, since the only way we get here is via SIGINT (or
; possibly SIGQUIT if/when I add logic for that).
;------------------------------------------------------------------------*/
  
  set_signal_handler(SIGINT,SIG_DFL);
  kill(getpid(),SIGINT);
  
  return EXIT_SUCCESS; /* keep this around to silence the compilers */
}

/*************************************************************/

Status ipv4_socket(const char *taddr)
{
  assert(taddr != NULL);
  
  if (g_maxsocket == MAX_SOCKETS)
    return retstatus(false,EADDRNOTAVAIL,"ipv4_socket()");
    
  g_ipv4 = g_maxsocket++;
  inet_pton(AF_INET,taddr,&g_sockets[g_ipv4].local.sin.sin_addr.s_addr);
  g_sockets[g_ipv4].local.sin.sin_family = AF_INET;
  g_sockets[g_ipv4].local.sin.sin_port = htons(LOG_PORT);
  return create_socket(&g_sockets[g_ipv4],sizeof(struct sockaddr_in));
}

/*************************************************************/

Status ipv6_socket(const char *taddr)
{
  assert(taddr != NULL);
  
  if (g_maxsocket == MAX_SOCKETS)
    return retstatus(false,EADDRNOTAVAIL,"ipv6_socket()");
  
  g_ipv6 = g_maxsocket++;
  inet_pton(AF_INET6,taddr,&g_sockets[g_ipv6].local.sin6.sin6_addr.s6_addr);
  g_sockets[g_ipv6].local.sin6.sin6_family = AF_INET6;
  g_sockets[g_ipv6].local.sin6.sin6_port = htons(LOG_PORT);
  return create_socket(&g_sockets[g_ipv6],sizeof(struct sockaddr_in6));
}

/**************************************************************/

Status local_socket(const char *name)
{
  Status status;
  mode_t oldmask;
  size_t local;
  size_t namelen;
  
  assert(name != NULL);
  
  if (g_maxsocket == MAX_SOCKETS)
    return retstatus(false,EADDRNOTAVAIL,"local_socket()");
    
  local = g_maxsocket++;
  oldmask = umask(0111);
  namelen = strlen(name);

  if (namelen > sizeof(struct sockaddr_un) - 1)
    namelen = sizeof(struct sockaddr_un) - 1;
    
  unlink(name);
  memset(&g_sockets[local].local,0,sizeof(struct sockaddr_un));
  memcpy(g_sockets[local].local.ssun.sun_path,name,namelen);
  g_sockets[local].local.ssun.sun_family = AF_LOCAL;
  status = create_socket(&g_sockets[local],sizeof(g_sockets[local].local.ssun));
  umask(oldmask);
  return status;
}

/*******************************************************************/

Status create_socket(SocketNode listen,socklen_t saddr)
{
  int reuse = 1;
  int flag;

  assert(listen != NULL);
  assert(saddr > 0);
  
  listen->len = saddr;
  listen->sock = socket(listen->local.ss.sa_family,SOCK_DGRAM,0);
  
  if (setsockopt(listen->sock,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) == -1)
    return retstatus(false,errno,"setsockopt(REUSE)");

  flag = fcntl(listen->sock,F_GETFL,0);
  if (flag == -1)
    return retstatus(false,errno,"fcntl(GETFL)");
  
  if (fcntl(listen->sock,F_SETFL,flag | O_NONBLOCK) == -1)
    return retstatus(false,errno,"fcntl(SETFL)");

  if (bind(listen->sock,&listen->local.ss,saddr) == -1)
    return retstatus(false,errno,"bind()");
    
  /*--------------------------------------------------------------------
; For multicast addresses, inform the kernel *not* to loop the data back
; to us. Syslog loops are *NOT* fun; trust me on this.
;
; Also, Stevens (from _Unix Network Programming: The Sockets Networking
; API_, Volume 1, Third Edition_) notes the following for
; IP_MULTICAST_LOOP:
;
; The IPv4 TTL and loopback options take a u_char argument, while the
; IPv6 hop limit and loopback options take an int and u_int argument,
; respectively. A common programming error with the IPv4 multicast
; options is to call setsockopt() with an int argument to specify the
; TTL or loopback (which is not allowed; pp. 354-355 of TCPv2), since
; most of the other socket options ... have integer arguments. The
; change with IPv6 makes them more consistent with other options.
;
; (via http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=156232)
;
; Also, check to see if the addresses are multicast addresses, and
; join in the appropriate multicast group.
;-----------------------------------------------------------------------*/

  if (listen->local.ss.sa_family == AF_INET)
  {
    if (IN_MULTICAST(ntohl(listen->local.sin.sin_addr.s_addr)))
    {
      unsigned char on = 0;
      struct ip_mreq mreq;

      if (setsockopt(listen->sock,IPPROTO_IP,IP_MULTICAST_LOOP,&on,1) < 0)
        return retstatus(false,errno,"setsockopt(MULTICAST_LOOP)");
      
      mreq.imr_multiaddr = listen->local.sin.sin_addr;
      mreq.imr_interface.s_addr = INADDR_ANY;
      if (setsockopt(listen->sock,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq)) < 0)
        return retstatus(false,errno,"setsockopt(MULTICAST)");
    }
  }
  else if (listen->local.ss.sa_family == AF_INET6)
  {
    if (IN6_IS_ADDR_MULTICAST(&listen->local.sin6.sin6_addr))
    {
      unsigned int on = 0;
      struct ipv6_mreq mreq6;

      if (setsockopt(listen->sock,IPPROTO_IPV6,IPV6_MULTICAST_LOOP,&on,sizeof(on)) < 0)
        return retstatus(false,errno,"setsockopt(MULTICAST6_LOOP)");
      
      mreq6.ipv6mr_multiaddr = listen->local.sin6.sin6_addr;
      mreq6.ipv6mr_interface = 0;
      if (setsockopt(listen->sock,IPPROTO_IPV6,IPV6_ADD_MEMBERSHIP,&mreq6,sizeof(mreq6)) < 0)
        return retstatus(false,errno,"setsockopt(MULTICAST6)");
    }
  }
  
  return c_okay;
}
 
/*****************************************************************/

void event_read(SocketNode sock)
{
  sockaddr_all remote;
  socklen_t remsize;
  ssize_t bytes;
  char buffer[65536uL]; /* 65507 max size of UDP packet */
  
  assert(sock != NULL);
  
  memset(&remote,0,sizeof(remote));
  remsize = sizeof(remote);
  bytes = recvfrom(sock->sock,buffer,sizeof(buffer),0,(struct sockaddr *)&remote,&remsize);
  
  if (bytes == -1)
  {
    if (errno == EINTR) return;
    internal_log(LOG_DEBUG,"recvfrom() = %s",strerror(errno));
    return;
  }
  
  assert(bytes >= 0);
  assert((size_t)bytes < sizeof(buffer));
  
  buffer[bytes] = '\0';
  
  for (size_t i = 0 ; buffer[i] != '\0'; i++)
    if (iscntrl(buffer[i]))
      buffer[i] = ' ';
  
  syslog_interp(&sock->local,&remote,buffer,&buffer[bytes]);
}

/*********************************************************************/

void syslog_interp(sockaddr_all *ploc,sockaddr_all *pss,const char *buffer,const char *end)
{
  struct msg msg;
  char host[FILENAME_MAX];
  char laddr[FILENAME_MAX];
  struct tm dateread;
  time_t now;
  int value;
  char *p;
  char *q;
  
  assert(ploc != NULL);
  assert(pss != NULL);
  assert(buffer != NULL);
  assert(end != NULL);
  assert(buffer <= end);
  
  now = time(NULL);
  localtime_r(&now,&dateread);
  
  msg.version = 0;
  msg.raw.size = (size_t)(end - buffer);
  msg.raw.text = buffer;
  msg.timestamp = now;
  msg.logtimestamp = now;
  msg.program = c_null;
  msg.relay = c_null;
  msg.pid = 0;
  
  if (pss->ss.sa_family == AF_INET)
  {
    inet_ntop(AF_INET,&pss->sin.sin_addr,host,INET_ADDRSTRLEN);
    
    msg.remote = true;
    msg.host.size = strlen(host);
    msg.host.text = host;
    msg.port = ntohs(pss->sin.sin_port);
  }
  else if (pss->ss.sa_family == AF_INET6)
  {
    inet_ntop(AF_INET6,&pss->sin6.sin6_addr,host,INET6_ADDRSTRLEN);
    
    msg.remote = true;
    msg.host.size = strlen(host);
    msg.host.text = host;
    msg.port = ntohs(pss->sin6.sin6_port);
  }
  else
  {
    msg.remote = false;
    msg.host.size = strlen(ploc->ssun.sun_path);
    msg.host.text = ploc->ssun.sun_path;
    msg.port = -1;
  }
  
  if (ploc->ss.sa_family == AF_INET)
  {
    inet_ntop(AF_INET,&ploc->sin.sin_addr,laddr,INET_ADDRSTRLEN);
    
    msg.laddr.size = strlen(laddr);
    msg.laddr.text = laddr;
    msg.lport = ntohs(ploc->sin.sin_port);
  }
  else if (ploc->ss.sa_family == AF_INET6)
  {
    inet_ntop(AF_INET6,&ploc->sin6.sin6_addr,laddr,INET6_ADDRSTRLEN);
    
    msg.laddr.size = strlen(laddr);
    msg.laddr.text = laddr;
    msg.lport = ntohs(ploc->sin6.sin6_port);
  }
  else
  {
    msg.laddr.size = strlen(ploc->ssun.sun_path);
    msg.laddr.text = ploc->ssun.sun_path;
    msg.lport = -1;
  }
  
  /*----------------------------------------------------------------------
; the use of explicit values for LOG_USER and LOG_NOTICE here is because
; the values defined are *not* the *direct* values---LOG_USER (in fact,
; all the defined facilities) are biased by a multiplier (8---at least on
; my system). Thus, the direct, non-#define'ed values used here.
;
; Also, if there's any problem parsing the front end of the message, we
; log the raw message we received using "user.notice".
;---------------------------------------------------------------------*/
  
  if (buffer[0] != '<')
  {
    msg.facility = 1; /* LOG_USER */
    msg.level = 5; /* LOG_NOTICE */
    msg.msg = msg.raw;
    
    process_msg(&msg);
    return;
  }
  
  value = strtoul(&buffer[1],&p,10);
  if (*p++ != '>')
  {
    msg.facility = 1; /* LOG_USER */
    msg.level = 5; /* LOG_NOTICE */
    msg.msg = msg.raw;
    
    process_msg(&msg);
    return;
  }
  
  msg.facility = value / 8;
  msg.level = value % 8;
  
  /*-------------------------------------------
; check to make sure our facility doesn't
; exceed what we're expecting
;-------------------------------------------*/
  
  if (msg.facility > 23) /* LOG_LOCAL7 */
  {
    msg.facility = 1; /* LOG_USER */
    msg.level = 5; /* LOG_NOTICE */
    msg.msg = msg.raw;
    
    process_msg(&msg);
    return;
  }
  
  /*---------------------------------------------
; check for a supplied timestamp.
;---------------------------------------------*/
  
  q = strptime(p,"%b %d %H:%M:%S",&dateread);
  
  if (q)
  {
    msg.logtimestamp = mktime(&dateread);
    if (*q != ' ')
    {
      msg.facility = 1; /* LOG_USER */
      msg.level = 5; /* LOG_NOTICE */
      msg.msg = msg.raw;

      process_msg(&msg);
      return;
    }
    p = q + 1;
  }
  
  /*------------------------------------------------------------------------
; check for origin field. We only look for IPv4/IPv6 literal addresses as
; that's the only reliable way to actually transmit such information (and
; parse it as intended per RFC3164). Thus, this code will fail if a
; hostname is sent, but it would also fail if the program name with an
; embedded space but no host is sent.
;
; I suppose I could check to see if the first white-space delimited field
; *only* contains alphanumberics, but then it could fail on a message that
; is sent that contains neither a host nor a program (which is possible).
;
; Pick your poison ... this works for me
;-------------------------------------------------------------------------*/

  assert(p <= end);
  q = memchr(p,' ',(size_t)(end - p));
  
  if (q)
  {
    size_t len = (size_t)(q - p);
    char addr[len + 1];
    unsigned char addrip[16]; /* big enough */
    int rc;
    
    memcpy(addr,p,len);
    addr[len] = '\0';
    
    rc = inet_pton(AF_INET6,addr,&addrip);
    if (rc == 1) /* valid IPv6 address */
    {
      msg.host.text = p;
      msg.host.size = len;
      p = q + 1;
    }
    else
    {
      rc = inet_pton(AF_INET,addr,&addrip);
      if (rc == 1) /* valid IPv4 address */
      {
        msg.host.text = p;
        msg.host.size = len;
        p = q + 1;
      }
    }
  }

  /*-----------------------------------------------------------------------
; check for program field. Quick and dirty check that works so far.
; Basically, we check for the ':' character, which appears to nearly
; always terminate this field. Then we check for the '[', which mostly
; (but not always) indicates the PID. This handles every case I've
; encounted so far, with program names with embedded spaces; that and PID
; field, what have you. It doesn't catch *all* PIDs, as there's a program
; (gconfd, I'm looking at you) that embed the PID in parenthesis along
; with the userid, but hey, if that's *really* important, we can handle it
; here.
;------------------------------------------------------------------------*/
  
  assert(p <= end);
  q = memchr(p,':',(size_t)(end - p));
  
  if (q)
  {
    char *b;
    
    b = memchr(p,'[',(size_t)(q - p));
    if (b)
      msg.pid = strtoul(b + 1,NULL,10);
    else
      b = q;
      
    msg.program.text = p;
    msg.program.size = (size_t)(b - p);
    
    for (p = q + 1 ; *p && isspace(*p) ; p++)
      ;
  }

  /*---------------------------------------------------
; whatever remains, however small, is the msg.
;---------------------------------------------------*/
  
  msg.msg.text = p;
  msg.msg.size = (size_t)(end - p);
  
  process_msg(&msg);
}

/***********************************************************************/

#ifdef CHECK_STRINGS
  static void clean_string(struct sysstring s)
  {
    assert(s.text != NULL);
    
    for (size_t i = 0 ; i < s.size ; i++)
      assert(isprint(s.text[i]));
  }
#endif

/**********************************************************************/

void process_msg(const struct msg *const pmsg)
{
  const char *err;
  int rc;
  
  assert(pmsg != NULL);

#ifdef CHECK_STRINGS
  clean_string(pmsg->raw);
  clean_string(pmsg->host);
  clean_string(pmsg->relay);
  clean_string(pmsg->laddr);
  clean_string(pmsg->program);
  clean_string(pmsg->msg);
#endif

  lua_getglobal(g_L,"log");
  lua_newtable(g_L);
  
  lua_pushinteger(g_L,pmsg->version);
  lua_setfield(g_L,-2,"version");
  
  lua_pushlstring(g_L,pmsg->raw.text,pmsg->raw.size);
  lua_setfield(g_L,-2,"_RAW");
  
  lua_pushlstring(g_L,pmsg->host.text,pmsg->host.size);
  lua_setfield(g_L,-2,"host");
  
  lua_pushlstring(g_L,pmsg->relay.text,pmsg->relay.size);
  lua_setfield(g_L,-2,"relay");
  
  lua_pushinteger(g_L,pmsg->port);
  lua_setfield(g_L,-2,"port");
  
  lua_pushboolean(g_L,pmsg->remote);
  lua_setfield(g_L,-2,"remote");
  
  lua_pushlstring(g_L,pmsg->laddr.text,pmsg->laddr.size);
  lua_setfield(g_L,-2,"localaddr");
  
  lua_pushinteger(g_L,pmsg->lport);
  lua_setfield(g_L,-2,"localport");
  
  lua_pushinteger(g_L,pmsg->timestamp);
  lua_setfield(g_L,-2,"timestamp");
  
  lua_pushinteger(g_L,pmsg->logtimestamp);
  lua_setfield(g_L,-2,"logtimestamp");
  
  lua_pushlstring(g_L,pmsg->program.text,pmsg->program.size);
  lua_setfield(g_L,-2,"program");
  
  lua_pushinteger(g_L,pmsg->pid);
  lua_setfield(g_L,-2,"pid");
  
  lua_pushstring(g_L,c_facility[pmsg->facility]);
  lua_setfield(g_L,-2,"facility");
  
  lua_pushstring(g_L,c_level[pmsg->level]);
  lua_setfield(g_L,-2,"level");
  
  lua_pushlstring(g_L,pmsg->msg.text,pmsg->msg.size);
  lua_setfield(g_L,-2,"msg");
  
  rc = lua_pcall(g_L,1,0,0);
  if (rc != 0)
  {
    err = lua_tostring(g_L,1);
    internal_log(LOG_ERR,"Lua ERROR(%d): %s",rc,err);
    lua_pop(g_L,1);
  }
}

/**********************************************************************/

Status globalv_init(int argc,char *argv[])
{
  static char luascript[FILENAME_MAX];
  Status status;
  int option = 0;
  
  assert(argc > 0);
  assert(argv != NULL);
  
  ListInit(&g_intlog);
  g_logtag.text = basename(argv[0]);
  g_logtag.size = strlen(g_logtag.text);
  
  for (size_t i = 0 ; i < MAX_SOCKETS; i++)
    g_sockets[i].sock = -1;

  opterr = 0; /* prevent getopt_long() from printing error message */
  
  while(true)
  {
    switch(getopt_long(argc,argv,"vfP46lhu:g:s:p:c:i:I:",c_options,&option))
    {
      default:
      case OPT_ERR:
           fprintf(stderr,"unknown option '%c'\n",optopt);
      case OPT_HELP:
           usage(argv[0]);
           return retstatus(false,0,"");
      case OPT_VERSION:
           fprintf(stderr,"%s " VERSION "\n",basename(argv[0]));
           return retstatus(false,0,"");
      case OPT_NONE:
           break;
      case OPT_FG:
           gf_foreground = true;
           break;
      case OPT_IPv4:
           status = ipv4_socket(LOG_IPv4);
           if (!status.okay) return status;
           break;
      case OPT_IPv4int:
           status = ipv4_socket(optarg);
           if (!status.okay) return status;
           break;
      case OPT_IPv6:
           status = ipv6_socket(LOG_IPv6);
           if (!status.okay) return status;
           break;
      case OPT_IPv6int:
           status = ipv6_socket(optarg);
           if (!status.okay) return status;
           break;
      case OPT_LOCAL:
           status = local_socket(LOG_LOCAL);
           if (!status.okay) return status;
           break;
      case OPT_SOCKET:
           status = local_socket(optarg);
           if (!status.okay) return status;
           break;
      case OPT_USER:
           g_user = optarg;
           break;
      case OPT_GROUP:
           g_group = optarg;
           break;
      case OPT_PATH:
           g_lpath = optarg;
           break;
      case OPT_CPATH:
           g_lcpath = optarg;
           break;
      case EOF:
           if (optind < argc)
           {
             if (argv[optind][0] == '/')
               g_luacode = argv[optind];
             else
             {
               /*---------------------------------------------------------
; this bit is here to turn a relative path into a full path
; based upon our current path. We do this because we change
; to the root dirctory when going into daemon mode. This
; way, we won't hang a possible unmount command because we're
; in some mounted directory---check comments in daemon_init()
; for more details.
;----------------------------------------------------------*/
      
               char cwd[FILENAME_MAX];
               char *path;
      
               path = getcwd(cwd,FILENAME_MAX);
               if (path == NULL)
                 return retstatus(false,errno,"getcwd()");

               snprintf(luascript,FILENAME_MAX,"%s/%s",path,argv[optind]);
               g_luacode = luascript;
             }
           }
           return c_okay;
    }
  }
}

/*****************************************************************/

void usage(const char *progname)
{
  assert(progname != NULL);
  
  fprintf(
   stderr,
        "usage: %s [options...] [luafile]\n"
        "\t-4 | --ip accept from IPv4 hosts\n"
        "\t | --ip4 \"\n"
        "\t | --ipv4 \"\n"
        "\t-i | --ipaddr <IPv4addr> accept IPv4 on IPv4addr\n"
        "\t | --ip4addr <IPv4addr> \"\n"
        "\t | --ipv4addr <IPv4addr> \"\n"
        "\t-6 | --ip6 accept from IPv6 hosts\n"
        "\t | --ipv6 \"\n"
        "\t-I | --ip6addr <IPv6addr> accept IPv6 on IPv6 addr\n"
        "\t | --ipv6addr <IPv6addr> \"\n"
        "\t-l | --local accept from " LOG_LOCAL "\n"
        "\t-s | --socket <path> accept from unix socket (%d max)\n"
        "\t-f | --foreground run in foreground\n"
        "\t-u | --user <username> user to run as (no default)\n"
        "\t-g | --group <groupname> group to run as (no default)\n"
        "\t-p | --lua-path <path> add path to Lua package.path\n"
        "\t-c | --lua-cpath <path> add path to Lua package.cpath\n"
        "\t-v | --version print version (" VERSION ") and quit\n"
        "\t-h | --help this message\n"
        "\n"
        "\t" LUA_CODE "\tdefault luafile\n"
        "\n",
        progname,
        MAX_SOCKETS - 3 /* possible IPv4, IPv6 and default local */
  );
}

/*******************************************************************/

Status drop_privs(void)
{
  if (g_user == NULL) /* if no user specified, we won't drop */
    return c_okay;
    
  if (getuid() != 0) /* if not root, we can't drop privs */
    return c_okay;

  long ubufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
  if (ubufsize < 0) ubufsize = BUFSIZ;
    
  char ubuffer[ubufsize];
  struct passwd uinfo;
  struct passwd *uresult;
  
  if (getpwnam_r(g_user,&uinfo,ubuffer,ubufsize,&uresult) != 0)
  {
    errno = 0;
    uinfo.pw_uid = strtoul(g_user,NULL,10);
    if (errno != 0)
      return retstatus(false,errno,"getpwnam_r()");
  }
  
  long gbufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
  if (gbufsize < 0) gbufsize = BUFSIZ;
  
  char gbuffer[gbufsize];
  struct group ginfo;
  struct group *gresult;
  
  if (g_group != NULL)
  {
    if (getgrnam_r(g_group,&ginfo,gbuffer,gbufsize,&gresult) != 0)
    {
      errno = 0;
      ginfo.gr_gid = strtoul(g_group,NULL,10);
      if (errno != 0)
        return retstatus(false,errno,"getgrnam_r()");
    }
  }
  else
    ginfo.gr_gid = uinfo.pw_gid;
  
  /*------------------------------------------------------------------------
; it's here we change the ownership of the PID file and any unix sockets.
; I don't care about the return value, as it may not even exist (because
; we might not have had perms to create it in the first place.
;------------------------------------------------------------------------*/
  
  chown(PID_FILE,uinfo.pw_uid,ginfo.gr_gid); /* don't care about results */

  for (size_t i = 0 ; i < g_maxsocket ; i++)
    if (g_sockets[i].local.ss.sa_family == AF_LOCAL)
      chown(g_sockets[i].local.ssun.sun_path,uinfo.pw_uid,ginfo.gr_gid);

  /*-------------------------------------------------------------------
; change our group id first, then user id. If we drop user id first,
; we may not be able to change our group id!
;-------------------------------------------------------------------*/

  if (setregid(ginfo.gr_gid,ginfo.gr_gid) == -1)
    return retstatus(false,errno,"setregid()");
    
  if (setreuid(uinfo.pw_uid,uinfo.pw_uid) == -1)
    return retstatus(false,errno,"getreuid()");
  
  internal_log(LOG_DEBUG,"dropped privs to %s:%s",g_user,g_group);
  return c_okay;
}

/*************************************************************************/

void load_script(void)
{
  int rc;
  
  rc = luaL_loadfile(g_L,g_luacode);
  if (rc != 0)
  {
    const char *err = lua_tostring(g_L,1);
    internal_log(LOG_DEBUG,"Lua ERROR: (%d) %s",rc,err);
    lua_pop(g_L,1);
    return;
  }
  
  rc = lua_pcall(g_L,0,LUA_MULTRET,0);
  if (rc != 0)
  {
    const char *err = lua_tostring(g_L,1);
    internal_log(LOG_ERR,"Lua ERROR: (%d) %s",rc,err);
    lua_pop(g_L,1);
    return;
  }
  
  internal_log(LOG_DEBUG,"loaded script %s",g_luacode);
}

/*************************************************************************/

Status daemon_init(void)
{
  pid_t pid;
  int fh;
  
  /*-----------------------------------------------------------------------
; From the Unix Programming FAQ (corraborated by Stevens):
;
; 1. 'fork()' so the parent can exit, this returns control to the command
; line or shell invoking your program. This step is required so that
; the new process is guaranteed not to be a process group leader. The
; next step, 'setsid()', fails if you're a process group leader.
;---------------------------------------------------------------------*/
             
  pid = fork();
  if (pid == (pid_t)-1)
    return retstatus(false,errno,"fork()");
  else if (pid != 0) /* parent goes bye bye */
    _exit(EXIT_SUCCESS);
  
  /*-------------------------------------------------------------------------
; 2. 'setsid()' to become a process group and session group leader. Since
; a controlling terminal is associated with a session, and this new
; session has not yet acquired a controlling terminal our process now
; has no controlling terminal, which is a Good Thing for daemons.
;
; _Advanced Programming in the Unix Environment_, 2nd Edition, also
; ignores SIGHUP. So adding that here as well.
;-----------------------------------------------------------------------*/

  setsid();
  set_signal_handler(SIGHUP,SIG_IGN); /* ignore this signal for now */

  /*-------------------------------------------------------------------------
; 3. 'fork()' again so the parent, (the session group leader), can exit.
; This means that we, as a non-session group leader, can never regain a
; controlling terminal.
;------------------------------------------------------------------------*/

  pid = fork();
  if (pid == (pid_t)-1)
    return retstatus(false,errno,"fork(2)");
  else if (pid != 0) /* parent goes bye bye */
    _exit(EXIT_SUCCESS);
  
  /*-------------------------------------------------------------------------
; 4. 'chdir("/")' to ensure that our process doesn't keep any directory in
; use. Failure to do this could make it so that an administrator
; couldn't unmount a filesystem, because it was our current directory.
;
; [Equivalently, we could change to any directory containing files
; important to the daemon's operation.]
;
; I just made sure the name of the script we are using contains the full
; path.
;-------------------------------------------------------------------------*/
            
  chdir("/");
  
  /*-----------------------------------------------------------------------
; 5. 'umask(022)' so that we have complete control over the permissions of
; anything we write. We don't know what umask we may have inherited.
;-----------------------------------------------------------------------*/

  umask(022);
  
  /*-----------------------------------------------------------------------
; 6. 'close()' fds 0, 1, and 2. This releases the standard in, out, and
; error we inherited from our parent process. We have no way of knowing
; where these fds might have been redirected to. Note that many daemons
; use 'sysconf()' to determine the limit '_SC_OPEN_MAX'.
; '_SC_OPEN_MAX' tells you the maximun open files/process. Then in a
; loop, the daemon can close all possible file descriptors. You have to
; decide if you need to do this or not. If you think that there might
; be file-descriptors open you should close them, since there's a limit
; on number of concurrent file descriptors.
;
; 7. Establish new open descriptors for stdin, stdout and stderr. Even if
; you don't plan to use them, it is still a good idea to have them
; open. The precise handling of these is a matter of taste; if you
; have a logfile, for example, you might wish to open it as stdout or
; stderr, and open '/dev/null' as stdin; alternatively, you could open
; '/dev/console' as stderr and/or stdout, and '/dev/null' as stdin, or
; any other combination that makes sense for your particular daemon.
;
; We do this here via dup2(), which combines steps 6 & 7.
;------------------------------------------------------------------------*/

  fh = open(DEV_NULL,O_RDWR);
  if (fh == -1)
    return retstatus(false,errno,"open(" DEV_NULL ")");
  
  assert(fh > 2);
  
  dup2(fh,STDIN_FILENO);
  dup2(fh,STDOUT_FILENO);
  dup2(fh,STDERR_FILENO);
  
  close(fh);
  
  return c_okay;
}
  
/***********************************************************************/

int map_str_to_int(const char *name,const char *const list[],size_t size)
{
  assert(name != NULL);
  assert(list != NULL);
  assert(size > 0);
  
  for (size_t i = 0 ; i < size ; i++)
  {
    if (strcmp(name,list[i]) == 0)
      return i;
  }
  return -1;
}

/***********************************************************************/

void handle_signal(int sig)
{
  switch(sig)
  {
    case SIGTERM: mf_sigint = 1; break;
    case SIGINT: mf_sigint = 1; break;
    case SIGUSR1: mf_sigusr1 = 1; break;
    case SIGHUP: mf_sighup = 1; break;
    case SIGALRM: mf_sigalarm = 1; break;
    default: break;
  }
}

/**********************************************************************/

Status set_signal_handler(int sig,void (*handler)(int))
{
  struct sigaction act;
  struct sigaction oact;
  
  sigemptyset(&act.sa_mask);
  act.sa_handler = handler;
  act.sa_flags = 0;
  
  if (sigaction(sig,&act,&oact) == -1)
    return retstatus(false,errno,"sigaction()");
  return c_okay;
}

/**************************************************************************/

int syslogintr_alarm(lua_State *L)
{
  struct itimerval set;
  int pcount;
  
  assert(L != NULL);
  
  pcount = lua_gettop(L);
  if (pcount == 0)
    return luaL_error(L,"not enough arguments");
  else if (pcount > 1)
    return luaL_error(L,"too many arguments");
  
  if (lua_isnumber(L,1))
    g_alarm = lua_tointeger(L,1);
  else if (lua_isstring(L,1))
  {
    const char *v = lua_tostring(L,1);
    char *p;
    
    g_alarm = strtoul(v,&p,10);
    switch(*p)
    {
      case 's': break;
      case 'm': g_alarm *= 60; break;
      case 'h': g_alarm *= 3600; break;
      case 'd': g_alarm *= 86400; break;
      default: break;
    }
  }
  else
    return luaL_error(L,"expected number or string");

  internal_log(LOG_DEBUG,"Alarm PID: %lu",(unsigned long)getpid());
  internal_log(LOG_DEBUG,"Alarm set for %d seconds",g_alarm);
  
  set.it_value.tv_sec = set.it_interval.tv_sec = g_alarm;
  set.it_value.tv_usec = set.it_interval.tv_usec = 0;
  
  if (setitimer(ITIMER_REAL,&set,NULL) == -1)
    internal_log(LOG_WARNING,"setitimer() = %s",strerror(errno));
  
  lua_pop(L,1);
  return 0;
}

/***********************************************************************/

int syslogintr_ud__toprint(lua_State *L)
{
  SocketNode paddr;
  char taddr[INET6_ADDRSTRLEN];
  const char *r;
  
  assert(L != NULL);
  
  paddr = luaL_checkudata(L,1,LUA_UD_HOST);
  lua_pop(L,1);
  
  switch(paddr->local.ss.sa_family)
  {
    case AF_INET:
         r = inet_ntop(AF_INET, &paddr->local.sin.sin_addr.s_addr,taddr,INET6_ADDRSTRLEN);
         break;
    case AF_INET6:
         r = inet_ntop(AF_INET6,&paddr->local.sin6.sin6_addr.s6_addr,taddr,INET6_ADDRSTRLEN);
         break;
    case AF_LOCAL:
         memcpy(taddr,paddr->local.ssun.sun_path,sizeof(paddr->local.ssun.sun_path));
         r = taddr;
         break;
    default:
         lua_pushnil(L);
         return 1;
  }
  
  if (r == NULL)
    lua_pushliteral(L,"");
  else
    lua_pushstring(L,taddr);

  return 1;
}

/*********************************************************************/

int syslogintr_host(lua_State *L)
{
  const char *hostname;
  struct addrinfo hints;
  struct addrinfo *results;
  SocketNode paddr;
  int rc;
  
  assert(L != NULL);
  
  hostname = luaL_checkstring(L,1);
  lua_pop(L,1);
  
  memset(&hints,0,sizeof(hints));
  hints.ai_flags = AI_NUMERICSERV;
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_DGRAM;
  
  rc = getaddrinfo(hostname,"514",&hints,&results);
  if (rc != 0)
  {
    internal_log(LOG_WARNING,"getaddrinfo(%s) = %s",hostname,strerror(errno));
    lua_pushnil(L);
    return 1;
  }
  
  paddr = lua_newuserdata(L,sizeof(struct socket_node));
  
  switch(results[0].ai_addr->sa_family)
  {
    case AF_INET:
         paddr->len = sizeof(struct sockaddr_in);
         paddr->sock = socket(AF_INET, SOCK_DGRAM,0);
         break;
    case AF_INET6:
         paddr->len = sizeof(struct sockaddr_in6);
         paddr->sock = socket(AF_INET6,SOCK_DGRAM,0);
         break;
    default:
         internal_log(LOG_WARNING,"unexpected family for address");
         freeaddrinfo(results);
         lua_pushnil(L);
         return 1;
  }
    
  luaL_getmetatable(L,LUA_UD_HOST);
  lua_setmetatable(L,-2);
  memcpy(&paddr->local,results[0].ai_addr,results[0].ai_addrlen);
  freeaddrinfo(results);
  return 1;
}

/************************************************************************/

int syslogintr_lsock(lua_State *L)
{
  SocketNode paddr;
  const char *unixsocket;
  size_t ussize;
  int sock;
  
  unixsocket = luaL_checklstring(L,1,&ussize);
  lua_pop(L,1);
  
  if (ussize > sizeof(paddr->local.ssun.sun_path))
  {
    internal_log(LOG_WARNING,"socket path too long");
    lua_pushnil(L);
    return 1;
  }
  
  sock = socket(AF_LOCAL,SOCK_DGRAM,0);
  if (sock == -1)
  {
    internal_log(LOG_ERR,"socket() = %s",strerror(errno));
    lua_pushnil(L);
    return 1;
  }
  
  paddr = lua_newuserdata(L,sizeof(struct socket_node));
  paddr->local.ssun.sun_family = AF_LOCAL;
  paddr->len = sizeof(struct sockaddr_un);
  paddr->sock = sock;
  memset(paddr->local.ssun.sun_path,0,sizeof(paddr->local.ssun.sun_path));
  memcpy(paddr->local.ssun.sun_path,unixsocket,ussize);
  
  luaL_getmetatable(L,LUA_UD_HOST);
  lua_setmetatable(L,-2);
  
  return 1;
}

/************************************************************************/

int syslogintr_relay(lua_State *L)
{
  SocketNode paddr;
  struct msg msg;
  struct tm stm;
  char date [16];
  char output[BUFSIZ];
  char *p;
  size_t max;
  size_t size;
  size_t dummy;
  
  assert(L != NULL);
  
  paddr = luaL_checkudata(L,1,LUA_UD_HOST);
  luaL_checktype(L,2,LUA_TTABLE);
  
  lua_getfield(L,2,"version");
  lua_getfield(L,2,"remote");
  lua_getfield(L,2,"host");
  lua_getfield(L,2,"logtimestamp");
  lua_getfield(L,2,"program");
  lua_getfield(L,2,"pid");
  lua_getfield(L,2,"facility");
  lua_getfield(L,2,"level");
  lua_getfield(L,2,"msg");
  
  msg.version = lua_tointeger(L,-9);
  msg.remote = lua_toboolean(L,-8);
  msg.host.text = lua_tolstring(L,-7,&msg.host.size);
  msg.logtimestamp = lua_tointeger(L,-6);
  msg.program.text = lua_tolstring(L,-5,&msg.program.size);
  msg.pid = lua_tointeger(L,-4);
  msg.facility = map_str_to_int(lua_tolstring(L,-3,&dummy),c_facility,MAX_FACILITY);
  msg.level = map_str_to_int(lua_tolstring(L,-2,&dummy),c_level, MAX_LEVEL);
  msg.msg.text = lua_tolstring(L,-1,&msg.msg.size);
  
  localtime_r(&msg.logtimestamp,&stm);
  strftime(date,sizeof(date),"%b %d %H:%M:%S",&stm);

  p = output;
  max = BUFSIZ;
  
  size = snprintf(p,max,"<%d>%s ",msg.facility * 8 + msg.level,date);
  max -= size;
  p += size;
  
  if (msg.remote)
  {
    assert(msg.host.size > 0);
    size = snprintf(p,max,"%s ",msg.host.text);
    max -= size;
    p += size;
  }
  
  if (msg.program.size)
  {
    if (msg.pid)
      size = snprintf(p,max,"%s[%d]",msg.program.text,msg.pid);
    else
      size = snprintf(p,max,"%s",msg.program.text);
    max -= size;
    p += size;
  }
  
  size = snprintf(p,max,": %s",msg.msg.text);
  size = (size_t)((p + size) - output);

  if (size > MAX_MSGLEN)
  {
    size = MAX_MSGLEN;
    output[size] = '\0';
  }
  
  if (sendto(paddr->sock,output,size,0,&paddr->local.ss,paddr->len) == -1)
    internal_log(LOG_ERR,"sendto() = %s",strerror(errno));
    
  /*----------------------------------------------------------------
; pop after we've used the data from Lua. Since Lua does
; garbage collection, if we pop the parameters before we
; use any string data, it may be collected and bad things
; would result.
;--------------------------------------------------------------*/
  
  lua_pop(L,11); /* 2 input, 9 fetches */
  return 0;
}

/************************************************************************/

void call_optional_luaf(const char *fname)
{
  lua_getglobal(g_L,fname);
  if (lua_isfunction(g_L,-1))
  {
    int rc = lua_pcall(g_L,0,0,0);
    if (rc != 0)
    {
      const char *err = lua_tostring(g_L,1);
      internal_log(LOG_ERR,"Lua ERROR: (%d) %s",rc,err);
      lua_pop(g_L,1);
    }
  }
  else if (!lua_isnil(g_L,-1))
  {
    internal_log(LOG_WARNING,"%s is type '%s' not type 'function'",fname,lua_typename(g_L,lua_type(g_L,1)));
    lua_pop(g_L,1);
  }
}

/**********************************************************************/

void internal_log(int priority,const char *format, ... )
{
  va_list args;
  char *buffer;
  struct msg *msg;
  int size;
  
  assert(priority > -1); /* min priority is 0 */
  assert(priority < 8); /* max priority level */
  assert(format != NULL);
  
  buffer = malloc(BUFSIZ);
  if (buffer == NULL)
    return;
    
  msg = malloc(sizeof(struct msg));
  if (msg == NULL)
  {
    free(buffer);
    return;
  }
  
  va_start(args,format);
  size = vsnprintf(buffer,BUFSIZ,format,args);
  va_end(args);
  
  msg->version = 0;
  msg->raw.size = size;
  msg->raw.text = buffer;
  msg->timestamp = time(NULL);
  msg->logtimestamp = msg->timestamp;
  msg->program = g_logtag;
  msg->relay = c_null;
  msg->pid = 0;
  msg->remote = false;
  msg->host.text = "(internal)";
  msg->host.size = 10; /* yeah, I know ... */
  msg->port = -1;
  msg->laddr.text = "(internal)";
  msg->laddr.size = 10;
  msg->lport = -1;
  msg->facility = 5; /* LOG_SYSLOG --- see comment in syslog_interp() */
  msg->level = priority;
  msg->msg.text = buffer;
  msg->msg.size = size;
  
  ListAddTail(&g_intlog,&msg->node);
}

/*************************************************************************/

Node *ListRemHead(List *const pl)
{
  Node *pn;
  
  assert(pl != NULL);
  
  pn = ListGetHead(pl);
  assert(pn != NULL);
  if (NodeValid(pn))
    NodeRemove(pn);
  return pn;
}

/*************************************************************************/

void NodeInsert(Node *const pn,Node *const pntoa)
{
  Node *pnn;
  
  assert(pn != NULL);
  assert(pntoa != NULL);
  
  pnn = pn->ln_Succ;
  pntoa->ln_Succ = pnn;
  pntoa->ln_Pred = pn;
  pn->ln_Succ = pntoa;
  pnn->ln_Pred = pntoa;
}

/************************************************************************/

void NodeRemove(Node *const pn)
{
  Node *pns;
  Node *pnp;
  
  assert(pn != NULL);
  assert(pn->ln_Succ != NULL);
  assert(pn->ln_Pred != NULL);
  
  pns = pn->ln_Succ;
  pnp = pn->ln_Pred;
  
  pns->ln_Pred = pnp;
  pnp->ln_Succ = pns;
}

/************************************************************************/

void add_lua_path(const char *name,const char *path)
{
  const char *currentpath;
  
  assert(name != NULL);
  assert(path != NULL);
  
  lua_getglobal(g_L,"package");
  lua_getfield(g_L,1,name);
  currentpath = lua_tostring(g_L,-1);
  lua_pop(g_L,1);
  
  lua_pushfstring(g_L,"%s;%s",path,currentpath);
  lua_setfield(g_L,1,name);
  lua_pop(g_L,1);
}

/*************************************************************************/
Something went wrong with that request. Please try again.