Changeset 511
- Timestamp:
- Oct 15, 2016, 10:00:38 PM (8 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/config.h.in
r491 r511 606 606 /* Define to 1 if you have the `hasmntopt' function. */ 607 607 #undef HAVE_HASMNTOPT 608 609 /* Define to 1 if you have the <ifaddrs.h> header file. */ 610 #undef HAVE_IFADDRS_H 608 611 609 612 /* Define to 1 if you have the `inet_aton' function. */ -
trunk/configure.ac
r509 r511 12 12 dnl start 13 13 dnl 14 AM_INIT_AUTOMAKE(samhain, 4.1. 5)14 AM_INIT_AUTOMAKE(samhain, 4.1.6) 15 15 AC_DEFINE([SAMHAIN], 1, [Application is samhain]) 16 16 AC_CANONICAL_HOST … … 245 245 sys/mman.h sys/param.h sys/inotify.h \ 246 246 sys/vfs.h mntent.h \ 247 sys/select.h sys/socket.h netinet/in.h \247 sys/select.h sys/socket.h netinet/in.h ifaddrs.h \ 248 248 regex.h glob.h fnmatch.h \ 249 249 linux/ext2_fs.h linux/fs.h ext2fs/ext2_fs.h asm/segment.h \ -
trunk/docs/Changelog
r509 r511 1 4.1.6: 2 * add portcheck option 'PortCheckDevice = device' to monitor a 3 device regardless of address assigned to it (patch by Anton H., plus 4 some additions) 5 * fix case sensitivity of severity/class options (issue raised by 6 Anton H.). 7 * clarify restrictions for ProcessCheckPSArg (user manual) 8 1 9 4.1.5: 2 10 * fix memory leak in server (reported by C. Doerr). -
trunk/src/samhain.c
r496 r511 1375 1375 else 1376 1376 { 1377 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MOD_OK,1377 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_OK, 1378 1378 _(modList[modnum].name)); 1379 1379 modList[modnum].initval = status; -
trunk/src/sh_portcheck.c
r498 r511 33 33 #include <stdio.h> 34 34 #include <string.h> 35 #include <stdlib.h> 35 36 #include <sys/types.h> 36 37 #include <sys/socket.h> 37 38 #include <netinet/in.h> 38 39 #include <arpa/inet.h> 40 #ifdef HAVE_IFADDRS_H 41 #include <ifaddrs.h> 42 #include <netdb.h> 43 #endif 39 44 #include <errno.h> 40 45 #include <unistd.h> … … 182 187 static int sh_portchk_add_interface (const char * str); 183 188 189 #if defined(HAVE_IFADDRS_H) 190 /* Exported interface to add an ethernet device 191 */ 192 static int sh_portchk_add_device (const char * str); 193 #endif 194 184 195 /* verify whether port/interface is blacklisted (do not check) 185 196 */ … … 286 297 sh_portchk_set_active, 287 298 }, 299 #if defined(HAVE_IFADDRS_H) 300 { 301 N_("portcheckdevice"), 302 sh_portchk_add_device, 303 }, 304 #endif 288 305 { 289 306 N_("portcheckinterface"), … … 1119 1136 */ 1120 1137 1121 #define SH_IFACE_MAX 16 1138 #define SH_IFACE_MAX 64 1139 #define SH_IFACE_ADDR 0 1140 #define SH_IFACE_DEV 1 1122 1141 1123 1142 struct portchk_interfaces { 1124 struct sh_sockaddr iface [SH_IFACE_MAX];1125 int used;1143 struct sh_sockaddr iface; 1144 int type; 1126 1145 }; 1127 1146 1128 static struct portchk_interfaces iface_list; 1147 static struct portchk_interfaces iface_list[SH_IFACE_MAX]; 1148 static int iface_list_used = 0; 1129 1149 static int iface_initialized = 0; 1130 1150 … … 1159 1179 if (iface_initialized == 0) 1160 1180 { 1161 iface_list .used = 0;1181 iface_list_used = 0; 1162 1182 iface_initialized = 1; 1163 1183 } … … 1167 1187 hent = sh_gethostbyname(portchk_hostname); 1168 1188 i = 0; 1169 while (hent && hent->h_addr_list[i] && (iface_list .used < SH_IFACE_MAX))1189 while (hent && hent->h_addr_list[i] && (iface_list_used < SH_IFACE_MAX)) 1170 1190 { 1171 1191 struct sockaddr_in sin; … … 1175 1195 sh_ipvx_save(&iface_tmp, AF_INET, (struct sockaddr *)&sin); 1176 1196 1177 for (j = 0; j < iface_list .used; ++j)1178 { 1179 if (0 == sh_ipvx_cmp(&iface_tmp, &(iface_list .iface[j])))1197 for (j = 0; j < iface_list_used; ++j) 1198 { 1199 if (0 == sh_ipvx_cmp(&iface_tmp, &(iface_list[j].iface))) 1180 1200 { 1181 1201 goto next_iface; … … 1183 1203 } 1184 1204 1185 sh_ipvx_save(&(iface_list .iface[iface_list.used]),1205 sh_ipvx_save(&(iface_list[iface_list_used].iface), 1186 1206 AF_INET, (struct sockaddr *)&sin); 1207 iface_list[iface_list_used].type = SH_IFACE_ADDR; 1187 1208 1188 1209 if (portchk_debug) 1189 1210 { 1190 1211 char buf[256]; 1191 sh_ipvx_ntoa(buf, sizeof(buf), &(iface_list .iface[iface_list.used]));1212 sh_ipvx_ntoa(buf, sizeof(buf), &(iface_list[iface_list_used].iface)); 1192 1213 fprintf(stderr, _("added interface[%d]: %s\n"), i, buf); 1193 1214 } 1194 ++iface_list .used;1215 ++iface_list_used; 1195 1216 1196 1217 next_iface: … … 1208 1229 struct sh_sockaddr iface_tmp; 1209 1230 1210 while ((p != NULL) && (iface_list .used < SH_IFACE_MAX))1231 while ((p != NULL) && (iface_list_used < SH_IFACE_MAX)) 1211 1232 { 1212 1233 sh_ipvx_save(&iface_tmp, p->ai_family, p->ai_addr); 1213 1234 1214 for (j = 0; j < iface_list .used; ++j)1235 for (j = 0; j < iface_list_used; ++j) 1215 1236 { 1216 1237 if (portchk_debug) 1217 1238 { 1218 1239 char buf1[256], buf2[256]; 1219 sh_ipvx_ntoa(buf1, sizeof(buf1), &(iface_list .iface[j]));1240 sh_ipvx_ntoa(buf1, sizeof(buf1), &(iface_list[j].iface)); 1220 1241 sh_ipvx_ntoa(buf2, sizeof(buf2), &iface_tmp); 1221 1242 fprintf(stderr, _("check interface[%d]: %s vs %s\n"), j, buf1, buf2); 1222 1243 } 1223 if (0 == sh_ipvx_cmp(&iface_tmp, &(iface_list .iface[j])))1244 if (0 == sh_ipvx_cmp(&iface_tmp, &(iface_list[j].iface))) 1224 1245 { 1225 1246 if (portchk_debug) … … 1228 1249 } 1229 1250 } 1230 sh_ipvx_save(&(iface_list .iface[iface_list.used]),1251 sh_ipvx_save(&(iface_list[iface_list_used].iface), 1231 1252 p->ai_family, p->ai_addr); 1232 1253 iface_list[iface_list_used].type = SH_IFACE_ADDR; 1233 1254 if (portchk_debug) 1234 1255 { 1235 1256 char buf[256]; 1236 sh_ipvx_ntoa(buf, sizeof(buf), &(iface_list .iface[iface_list.used]));1237 fprintf(stderr, _("added interface[%d]: %s\n"), iface_list .used, buf);1257 sh_ipvx_ntoa(buf, sizeof(buf), &(iface_list[iface_list_used].iface)); 1258 fprintf(stderr, _("added interface[%d]: %s\n"), iface_list_used, buf); 1238 1259 } 1239 1260 1240 ++iface_list .used;1261 ++iface_list_used; 1241 1262 1242 1263 next_iface: … … 1247 1268 #endif 1248 1269 1249 for (i = 0; i < iface_list .used; ++i)1250 { 1251 sh_ipvx_ntoa(ipbuf, sizeof(ipbuf), &(iface_list .iface[i]));1270 for (i = 0; i < iface_list_used; ++i) 1271 { 1272 sh_ipvx_ntoa(ipbuf, sizeof(ipbuf), &(iface_list[i].iface)); 1252 1273 sl_snprintf(errbuf, sizeof(errbuf), _("added interface: %s"), ipbuf); 1253 1274 … … 1285 1306 (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE)) 1286 1307 { 1308 (void) sh_portchk_init_internal(); 1287 1309 return SH_MOD_THREAD; 1288 1310 } … … 1291 1313 } 1292 1314 1293 1315 static void dev_list_kill(); 1294 1316 1295 1317 #if !defined(TEST_ONLY) … … 1305 1327 sh_portchk_maxport = -1; 1306 1328 1329 dev_list_kill(); 1330 1307 1331 portlist_udp = sh_portchk_kill_list (portlist_udp); 1308 1332 portlist_tcp = sh_portchk_kill_list (portlist_tcp); … … 1345 1369 /* Check all interfaces for this host 1346 1370 */ 1347 while (i < iface_list .used)1348 { 1349 memcpy(&paddr, &(iface_list .iface[i]), sizeof(paddr));1371 while (i < iface_list_used) 1372 { 1373 memcpy(&paddr, &(iface_list[i].iface), sizeof(paddr)); 1350 1374 1351 1375 if (paddr.ss_family != domain) … … 1577 1601 void * sh_dummy_1564_str = NULL; /* fix clobbered by.. warning */ 1578 1602 1579 static int sh_portchk_add_interface (const char * str)1603 static int sh_portchk_add_interface_int (const char * str, int type) 1580 1604 { 1581 1605 struct sh_sockaddr saddr; … … 1587 1611 if (iface_initialized == 0) 1588 1612 { 1589 iface_list .used = 0;1613 iface_list_used = 0; 1590 1614 iface_initialized = 1; 1591 1615 } … … 1609 1633 return -1; 1610 1634 1611 if (iface_list .used == SH_IFACE_MAX)1635 if (iface_list_used == SH_IFACE_MAX) 1612 1636 return -1; 1613 1637 … … 1619 1643 SH_MUTEX_UNLOCK(mutex_thread_nolog); 1620 1644 1621 memcpy (&(iface_list.iface[iface_list.used]), &(saddr), sizeof(saddr)); 1622 ++iface_list.used; 1645 memcpy (&(iface_list[iface_list_used].iface), &(saddr), sizeof(saddr)); 1646 iface_list[iface_list_used].type = type; 1647 ++iface_list_used; 1623 1648 } 1624 1649 } while (*str); … … 1627 1652 return 0; 1628 1653 } 1654 1655 static int sh_portchk_add_interface (const char * str) 1656 { 1657 return sh_portchk_add_interface_int (str, SH_IFACE_ADDR); 1658 } 1659 1660 #if defined(HAVE_IFADDRS_H) 1661 /* 1662 * subroutines to add a device 1663 */ 1664 void * sh_dummy_1651_ifa = NULL; /* fix clobbered by.. warning */ 1665 1666 static int portchk_add_device_int (const char * buf) 1667 { 1668 struct ifaddrs *ifaddr, *ifa; 1669 int family; 1670 #ifndef NI_MAXHOST 1671 #define NI_MAXHOST 1025 1672 #endif 1673 char host[NI_MAXHOST]; 1674 1675 sh_dummy_1651_ifa = (void*) &ifa; 1676 1677 if (getifaddrs(&ifaddr) == -1) 1678 { 1679 volatile int nerr = errno; 1680 char errbuf[SH_ERRBUF_SIZE]; 1681 sh_error_message(errno, errbuf, sizeof(errbuf)); 1682 SH_MUTEX_LOCK(mutex_thread_nolog); 1683 sh_error_handle((-1), FIL__, __LINE__, nerr, MSG_E_SUBGEN, 1684 errbuf, _("getifaddrs")); 1685 SH_MUTEX_UNLOCK(mutex_thread_nolog); 1686 return -1; 1687 } 1688 1689 for ( ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) 1690 { 1691 if (ifa->ifa_addr == NULL) 1692 continue; 1693 1694 if (strcmp(ifa->ifa_name, buf) == 0) 1695 { 1696 volatile int s = 0; 1697 family = ifa->ifa_addr->sa_family; 1698 1699 if (family == AF_INET) 1700 { 1701 s = getnameinfo( ifa->ifa_addr, sizeof(struct sockaddr_in), 1702 host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST ); 1703 1704 if (s == 0) 1705 { 1706 if (sh_portchk_add_interface_int(host, SH_IFACE_DEV) < 0) 1707 { 1708 freeifaddrs(ifaddr); 1709 return -1; 1710 } 1711 } 1712 } 1713 1714 #if defined(USE_IPVX) 1715 if (family == AF_INET6) 1716 { 1717 s = getnameinfo( ifa->ifa_addr, sizeof(struct sockaddr_in6), 1718 host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST ); 1719 1720 if (s == 0) 1721 { 1722 if (sh_portchk_add_interface_int(host, SH_IFACE_DEV) < 0) 1723 { 1724 freeifaddrs(ifaddr); 1725 return -1; 1726 } 1727 } 1728 } 1729 #endif 1730 1731 if (s != 0) 1732 { 1733 char errbuf[SH_ERRBUF_SIZE]; 1734 sl_strlcpy(errbuf, buf, sizeof(errbuf)); 1735 sl_strlcat(errbuf, ": ", sizeof(errbuf)); 1736 sl_strlcat(errbuf, gai_strerror(s), sizeof(errbuf)); 1737 SH_MUTEX_LOCK(mutex_thread_nolog); 1738 sh_error_handle((-1), FIL__, __LINE__, s, MSG_E_SUBGEN, 1739 errbuf, _("getnameinfo")); 1740 SH_MUTEX_UNLOCK(mutex_thread_nolog); 1741 } 1742 1743 } 1744 } 1745 1746 freeifaddrs(ifaddr); 1747 return 0; 1748 } 1749 1750 struct added_dev { 1751 char dev[64]; 1752 struct added_dev * next; 1753 }; 1754 1755 static struct added_dev * dev_list = NULL; 1756 1757 static void dev_list_add (char * buf) 1758 { 1759 struct added_dev * new = SH_ALLOC(sizeof(struct added_dev)); 1760 sl_strlcpy(new->dev, buf, 64); 1761 new->next = dev_list; 1762 dev_list = new; 1763 return; 1764 } 1765 1766 static void dev_list_kill () 1767 { 1768 struct added_dev * old; 1769 struct added_dev * new = dev_list; 1770 dev_list = NULL; 1771 1772 while (new) 1773 { 1774 old = new; 1775 new = new->next; 1776 SH_FREE(old); 1777 } 1778 return; 1779 } 1780 1781 static int sh_portchk_add_device (const char * str) 1782 { 1783 char buf[64]; 1784 1785 do { 1786 1787 while (*str == ',' || *str == ' ' || *str == '\t') ++str; 1788 1789 if (*str) 1790 { 1791 unsigned int i = 0; 1792 while (*str && i < (sizeof(buf)-1) && 1793 *str != ',' && *str != ' ' && *str != '\t') { 1794 buf[i] = *str; ++str; ++i; 1795 } 1796 buf[i] = '\0'; 1797 1798 if (portchk_add_device_int (buf) < 0) 1799 return -1; 1800 1801 dev_list_add(buf); 1802 } 1803 } while (*str); 1804 1805 return 0; 1806 } 1807 1808 static int iface_comp (const void *a, const void *b) 1809 { 1810 const struct portchk_interfaces * aa = (struct portchk_interfaces *) a; 1811 const struct portchk_interfaces * bb = (struct portchk_interfaces *) b; 1812 return (aa->type - bb->type); 1813 } 1814 1815 static void iface_qsort() 1816 { 1817 qsort(&iface_list[0], iface_list_used, sizeof(struct portchk_interfaces), 1818 iface_comp); 1819 return; 1820 } 1821 1822 static void recheck_devices() 1823 { 1824 if (dev_list) 1825 { 1826 struct added_dev * dev = dev_list; 1827 int i, j; 1828 1829 if (portchk_debug) 1830 { 1831 for (j = 0; j < iface_list_used; ++j) 1832 { 1833 char buf[SH_IP_BUF]; 1834 struct portchk_interfaces * aa = &(iface_list[j]); 1835 sh_ipvx_ntoa(buf, sizeof(buf), &(aa->iface)); 1836 fprintf(stderr, _("presort: iface[%d] type(%d) %s\n"), j, iface_list[j].type, buf); 1837 } 1838 } 1839 1840 iface_qsort(); 1841 1842 if (portchk_debug) 1843 { 1844 for (j = 0; j < iface_list_used; ++j) 1845 { 1846 char buf[SH_IP_BUF]; 1847 struct portchk_interfaces * aa = &(iface_list[j]); 1848 sh_ipvx_ntoa(buf, sizeof(buf), &(aa->iface)); 1849 fprintf(stderr, _("postsor: iface[%d] type(%d) %s\n"), j, iface_list[j].type, buf); 1850 } 1851 } 1852 1853 i = 0; 1854 for (j = 0; j < iface_list_used; ++j) 1855 if (iface_list[j].type == SH_IFACE_DEV) ++i; 1856 iface_list_used -= i; 1857 1858 if (portchk_debug) 1859 { 1860 for (j = 0; j < iface_list_used; ++j) 1861 { 1862 char buf[SH_IP_BUF]; 1863 struct portchk_interfaces * aa = &(iface_list[j]); 1864 sh_ipvx_ntoa(buf, sizeof(buf), &(aa->iface)); 1865 fprintf(stderr, _("postdel: iface[%d] type(%d) %s\n"), j, iface_list[j].type, buf); 1866 } 1867 } 1868 1869 while (dev) 1870 { 1871 portchk_add_device_int (dev->dev); 1872 dev = dev->next; 1873 } 1874 } 1875 return; 1876 } 1877 #endif 1629 1878 1630 1879 /* verify whether port/interface is blacklisted (do not check) … … 1928 2177 1929 2178 sh_portchk_reset_lists(); 2179 2180 #if defined(HAVE_IFADDRS_H) 2181 recheck_devices(); 2182 #endif 2183 1930 2184 if ((0 != geteuid()) && (min_port < 1024)) 1931 2185 {
Note:
See TracChangeset
for help on using the changeset viewer.