Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process ARP data for new network table #455

Merged
merged 30 commits into from
Jan 9, 2019
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
36d1499
Add network table. This will update the database to version 3. Furthe…
DL6ER Dec 23, 2018
40fd4d3
Add / update rows in table network depending on what we see in the AR…
DL6ER Dec 24, 2018
c7bdf9b
Update if device uses Pi-hole and store host name if available
DL6ER Dec 25, 2018
050452e
Merge branch 'fix/shm_lock_resolve' into new/hosts-table
DL6ER Dec 25, 2018
82b43c3
Parse ARP cache after storing queries into long-term database(usen th…
DL6ER Dec 25, 2018
95d3c02
Store lastQuery property for known clients
DL6ER Dec 25, 2018
68a584a
Reduce code duplication
DL6ER Dec 26, 2018
465a490
Use one big transaction for changes to be written to the network tabl…
DL6ER Dec 26, 2018
3a8c879
Initialize new devices with lastQuery property if available
DL6ER Dec 26, 2018
54253bd
Added more comments
DL6ER Dec 26, 2018
ac6ee9e
Add config option to disable ARP cache parsing
DL6ER Dec 26, 2018
d8fdf76
Store number of queries per client in the database
DL6ER Dec 28, 2018
f2f543c
Do not count in findClient() if search is triggered from parse_arp_ca…
DL6ER Dec 28, 2018
92d8198
Add MAC->Vendor database support. We will provide the database as an …
DL6ER Dec 30, 2018
a2b7a24
Add routine to update all existing network table entries using the la…
DL6ER Dec 30, 2018
bd749f4
Added python3 script to automatically generate the macvendor database…
DL6ER Dec 31, 2018
85e954f
Optimize database after creation (reduces filesize by about 10%) + do…
DL6ER Dec 31, 2018
01aeead
Merge branch 'development' into new/network-details
DL6ER Jan 1, 2019
8ad22b2
Mac vendor database generator: Remove quotation marks as these might …
DL6ER Jan 1, 2019
7714b85
Merge branch 'development' into new/network-details
DL6ER Jan 6, 2019
262fd8e
Improve python aux script
DL6ER Jan 6, 2019
0f6c520
Remove obsolete dependency
DL6ER Jan 7, 2019
0742d4c
Use simple cast of sqlite3_column_text() instead of using asprintf()
DL6ER Jan 7, 2019
078b2ff
Free memory when breaking
DL6ER Jan 7, 2019
d01d660
Separate error messages into two if-statements
DL6ER Jan 7, 2019
9e66965
Change code to ensure hostname is always set
DL6ER Jan 7, 2019
486e497
Review comments
DL6ER Jan 7, 2019
427f8ad
Further review comments. Always allocate vendor so we can always free…
DL6ER Jan 7, 2019
054dfce
Check for hostname != NULL is obsolete
DL6ER Jan 7, 2019
787cf11
Add generated aux files to .gitignore
DL6ER Jan 8, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions FTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
#define MAXITER 1000

// FTLDNS enums
enum { DATABASE_WRITE_TIMER, EXIT_TIMER, GC_TIMER, LISTS_TIMER, REGEX_TIMER };
enum { DATABASE_WRITE_TIMER, EXIT_TIMER, GC_TIMER, LISTS_TIMER, REGEX_TIMER, ARP_TIMER, LAST_TIMER };
enum { QUERIES, FORWARDED, CLIENTS, DOMAINS, OVERTIME, WILDCARD };
enum { DNSSEC_UNSPECIFIED, DNSSEC_SECURE, DNSSEC_INSECURE, DNSSEC_BOGUS, DNSSEC_ABANDONED, DNSSEC_UNKNOWN };
enum { QUERY_UNKNOWN, QUERY_GRAVITY, QUERY_FORWARDED, QUERY_CACHE, QUERY_WILDCARD, QUERY_BLACKLIST, QUERY_EXTERNAL_BLOCKED };
Expand All @@ -80,6 +80,11 @@ enum { MODE_IP, MODE_NX, MODE_NULL, MODE_IP_NODATA_AAAA, MODE_NODATA };
enum { REGEX_UNKNOWN, REGEX_BLOCKED, REGEX_NOTBLOCKED };
enum { BLOCKING_DISABLED, BLOCKING_ENABLED, BLOCKING_UNKNOWN };

// Database table "ftl"
enum { DB_VERSION, DB_LASTTIMESTAMP, DB_FIRSTCOUNTERTIMESTAMP };
// Database table "counters"
enum { DB_TOTALQUERIES, DB_BLOCKEDQUERIES };

// Privacy mode constants
#define HIDDEN_DOMAIN "hidden"
#define HIDDEN_CLIENT "0.0.0.0"
Expand All @@ -93,6 +98,7 @@ typedef struct {
char* port;
char* db;
char* socketfile;
char* macvendordb;
} FTLFileNamesStruct;

typedef struct {
Expand Down Expand Up @@ -144,6 +150,7 @@ typedef struct {
bool regex_debugmode;
bool analyze_only_A_AAAA;
bool DBimport;
bool parse_arp_cache;
} ConfigStruct;

// Dynamic structs
Expand Down Expand Up @@ -182,6 +189,8 @@ typedef struct {
unsigned long long ippos;
unsigned long long namepos;
bool new;
time_t lastQuery;
unsigned int numQueriesARP;
} clientsDataStruct;

typedef struct {
Expand All @@ -208,11 +217,15 @@ typedef struct {
} whitelistStruct;

// Prepare timers, used mainly for debugging purposes
#define NUMTIMERS 5
#define NUMTIMERS LAST_TIMER

// Used to check memory integrity in various structs
#define MAGICBYTE 0x57

// Some magic database constants
#define DB_FAILED -2
#define DB_NODATA -1

extern logFileNamesStruct files;
extern FTLFileNamesStruct FTLfiles;
extern countersStruct *counters;
Expand Down Expand Up @@ -250,7 +263,6 @@ extern long int lastdbindex;
extern bool travis;
extern bool DBdeleteoldqueries;
extern bool rereadgravity;
extern long int lastDBimportedtimestamp;
extern bool ipv4telnet, ipv6telnet;
extern bool istelnet[MAXCONNS];

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ DNSMASQOPTS = -DHAVE_DNSSEC -DHAVE_DNSSEC_STATIC
# Flags for compiling with libidn2: -DHAVE_LIBIDN2 -DIDN2_VERSION_NUMBER=0x02000003

FTLDEPS = FTL.h routines.h version.h api.h dnsmasq_interface.h shmem.h
FTLOBJ = main.o memory.o log.o daemon.o datastructure.o signals.o socket.o request.o grep.o setupVars.o args.o gc.o config.o database.o msgpack.o api.o dnsmasq_interface.o resolve.o regex.o shmem.o capabilities.o
FTLOBJ = main.o memory.o log.o daemon.o datastructure.o signals.o socket.o request.o grep.o setupVars.o args.o gc.o config.o database.o msgpack.o api.o dnsmasq_interface.o resolve.o regex.o shmem.o capabilities.o networktable.o

DNSMASQDEPS = config.h dhcp-protocol.h dns-protocol.h radv-protocol.h dhcp6-protocol.h dnsmasq.h ip6addr.h metrics.h ../dnsmasq_interface.h
DNSMASQOBJ = arp.o dbus.o domain.o lease.o outpacket.o rrfilter.o auth.o dhcp6.o edns0.o log.o poll.o slaac.o blockdata.o dhcp.o forward.o loop.o radv.o tables.o bpf.o dhcp-common.o helper.o netlink.o rfc1035.o tftp.o cache.o dnsmasq.o inotify.o network.o rfc2131.o util.o conntrack.o dnssec.o ipset.o option.o rfc3315.o crypto.o dump.o ubus.o metrics.o
Expand Down
83 changes: 83 additions & 0 deletions aux/macvendor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Pi-hole: A black hole for Internet advertisements
# (c) 2019 Pi-hole, LLC (https://pi-hole.net)
# Network-wide ad blocking via your own hardware.
#
# FTL Engine - auxiliary files
# MAC -> Vendor database generator
#
# This is a python3 script
#
# This file is copyright under the latest version of the EUPL.
# Please see LICENSE file for your rights under this license.

import os
import re
import urllib.request
import sqlite3

# Download raw data from Wireshark's website
# We use the official URL recommended in the header of this file
print("Downloading...")
urllib.request.urlretrieve("https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=manuf", "manuf.data")
DL6ER marked this conversation as resolved.
Show resolved Hide resolved
print("...done")

# Read file into memory and process lines
manuf = open("manuf.data", "r")
data = []
print("Processing...")
for line in manuf:
line = line.strip()

# Skip comments and empty lines
if line[1] == "#" or line == "":
continue

# Remove quotation marks as these might interfere with later INSERT / UPDATE commands
line = re.sub("\'|\"","", line)
# \s = Unicode whitespace characters, including [ \t\n\r\f\v]
cols = re.split("\s\s+|\t", line)
# Use try/except chain to catch empty/incomplete lines without failing hard
try:
# Strip whitespace and quotation marks (some entries are incomplete and cause errors with the CSV parser otherwise)
mac = cols[0].strip().strip("\"")
except:
continue
try:
desc_short = cols[1].strip().strip("\"")
except:
desc_short = ""
try:
desc_long = cols[2].strip().strip("\"")
except:
desc_long = ""

# Only add long description where available
# There are a few vendors for which only the
# short description field is used
if(desc_long):
data.append([mac, desc_long])
else:
data.append([mac, desc_short])
print("...done")
manuf.close()

# Create database
database = "macvendor.db"

# Try to delete old database file, pass if no old file exists
try:
os.remove(database)
except OSError:
pass

print("Generating database...")
con = sqlite3.connect(database)
cur = con.cursor()
cur.execute("CREATE TABLE macvendor (mac TEXT NOT NULL, vendor TEXT NOT NULL, PRIMARY KEY (mac))")
cur.executemany("INSERT INTO macvendor (mac, vendor) VALUES (?, ?);", data)
con.commit()
print("...done.")
print("Optimizing database...")
con.execute("VACUUM")
print("...done")
print("Lines inserted into database:", cur.rowcount)
16 changes: 16 additions & 0 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,22 @@ void read_FTLconf(void)
// AUDITLISTFILE
getpath(fp, "AUDITLISTFILE", "/etc/pihole/auditlog.list", &files.auditlist);

// MACVENDORDB
getpath(fp, "MACVENDORDB", "/etc/pihole/macvendor.db", &FTLfiles.macvendordb);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requires documentation update.


// PARSE_ARP_CACHE
// defaults to: true
config.parse_arp_cache = true;
buffer = parse_FTLconf(fp, "PARSE_ARP_CACHE");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requires documentation update.


if(buffer != NULL && strcasecmp(buffer, "false") == 0)
config.parse_arp_cache = false;

if(config.parse_arp_cache)
logg(" PARSE_ARP_CACHE: Active");
else
logg(" PARSE_ARP_CACHE: Inactive");

logg("Finished config file parsing");

// Release memory
Expand Down
Loading