]> git.ipfire.org Git - ipfire-2.x.git/blame - src/patches/dnsmasq/0056-New-version-of-contrib-reverse-dns.patch
Merge remote-tracking branch 'earl/tor' into next
[ipfire-2.x.git] / src / patches / dnsmasq / 0056-New-version-of-contrib-reverse-dns.patch
CommitLineData
e5f58910
MT
1From 4c960fa90a975d20f75a1ecabd217247f1922c8f Mon Sep 17 00:00:00 2001
2From: Simon Kelley <simon@thekelleys.org.uk>
3Date: Wed, 4 Mar 2015 20:32:26 +0000
efbd3a9a 4Subject: [PATCH 56/98] New version of contrib/reverse-dns
e5f58910
MT
5
6---
7 contrib/reverse-dns/README | 22 +++---
8 contrib/reverse-dns/reverse_replace.sh | 131 ++++++++++++++++++++++++++++-----
9 2 files changed, 125 insertions(+), 28 deletions(-)
10
11diff --git a/contrib/reverse-dns/README b/contrib/reverse-dns/README
12index f87eb77c4c22..2ec4df1f957e 100644
13--- a/contrib/reverse-dns/README
14+++ b/contrib/reverse-dns/README
15@@ -1,18 +1,18 @@
16-Hi.\r
17+The script reads stdin and replaces all IP addresses with names before\r
18+outputting it again. IPs from private networks are reverse looked up\r
19+via dns. Other IP adresses are searched for in the dnsmasq query log.\r
20+This gives names (CNAMEs if I understand DNS correctly) that are closer\r
21+to the name the client originally asked for then the names obtained by\r
22+reverse lookup. Just run\r
23 \r
24-To translate my routers netstat-nat output into names that actually talk\r
25-to me I have started writing to simple shell scripts. They require \r
26+netstat -n -4 | ./reverse_replace.sh \r
27+\r
28+to see what it does. It needs \r
29 \r
30 log-queries\r
31 log-facility=/var/log/dnsmasq.log\r
32 \r
33-to be set. With\r
34-\r
35-netstat-nat -n -4 | reverse_replace.sh \r
36-\r
37-I get retranslated output.\r
38-\r
39-Sincerely,\r
40-Joachim\r
41+in the dnsmasq configuration.\r
42 \r
43+The script runs on debian (with ash installed) and on busybox.\r
44 \r
45diff --git a/contrib/reverse-dns/reverse_replace.sh b/contrib/reverse-dns/reverse_replace.sh
46index a11c164b7f19..5b4aebd71456 100644
47--- a/contrib/reverse-dns/reverse_replace.sh
48+++ b/contrib/reverse-dns/reverse_replace.sh
49@@ -1,28 +1,125 @@
50-#!/bin/bash
51-# $Id: reverse_replace.sh 4 2015-02-17 20:14:59Z jo $
52+#!/bin/ash
53+# $Id: reverse_replace.sh 18 2015-03-01 16:12:35Z jo $
54 #
55 # Usage e.g.: netstat -n -4 | reverse_replace.sh
56 # Parses stdin for IP4 addresses and replaces them
57-# with names retrieved by reverse_dns.sh
58+# with names retrieved by parsing the dnsmasq log.
59+# This currently only gives CNAMEs. But these
60+# usually tell ou more than the mones from reverse
61+# lookups.
62+#
63+# This has been tested on debian and asuswrt. Plese
64+# report successful tests on other platforms.
65+#
66+# Author: Joachim Zobel <jz-2014@heute-morgen.de>
67+# License: Consider this MIT style licensed. You can
68+# do as you ike, but you must not remove my name.
69 #
70
71-DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
72-DNS=$DIR/reverse_dns.sh
73+LOG=/var/log/dnsmasq.log
74+MAX_LINES=15000
75
76-# sed regex
77+# sed regex do match IPs
78 IP_regex='[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'
79+# private IP ranges
80+IP_private='\(^127\.\)\|\(^192\.168\.\)\|\(^10\.\)\|\(^172\.1[6-9]\.\)\|\(^172\.2[0-9]\.\)\|\(^172\.3[0-1]\.\)'
81
82-while read LINE; do
83- if grep --quiet $IP_regex <<< "$LINE"; then
84- IPs=`sed "s#.*\b\($IP_regex\)\b.*#\1 #g" <<< "$LINE"`
85- IPs=($IPs)
86- for IP in "${IPs[@]}"
87- do
88- NAME=`$DNS $IP`
89- # echo "$NAME is $IP";
90- LINE="${LINE/$IP/$NAME}"
91- done
92+#######################################################################
93+# Find Commands
94+
95+HOST=nslookup
96+if type host > /dev/null 2>&1; then
97+ # echo "No need for nslookup, host is there"
98+ HOST=host
99+fi
100+
101+#######################################################################
102+# Functions
103+
104+# Use shell variables for an (IP) lookup table
105+create_lookup_table()
106+{
107+ # Parse log into lookup table
108+ local CMDS="$( tail -"$MAX_LINES" "$LOG" | \
109+ grep " is $IP_regex" | \
110+ sed "s#.* \([^ ]*\) is \($IP_regex\).*#set_val \2 \1;#" )"
111+
112+ local IFS='
113+'
114+ for CMD in $CMDS
115+ do
116+ eval $CMD
117+ done
118+}
119+
120+set_val()
121+{
122+ local _IP=$(echo $1 | tr . _)
123+ local KEY="__IP__$_IP"
124+ eval "$KEY"=$2
125+}
126+
127+get_val()
128+{
129+ local _IP=$(echo $1 | tr . _)
130+ local KEY="__IP__$_IP"
131+ eval echo -n '${'"$KEY"'}'
132+}
133+
134+dns_lookup()
135+{
136+ local IP=$1
137+
138+ local RTN="$($HOST $IP | \
139+ sed 's#\s\+#\n#g' | \
140+ grep -v '^$' | \
141+ tail -1 | tr -d '\n' | \
142+ sed 's#\.$##')"
143+ if echo $RTN | grep -q NXDOMAIN; then
144+ echo -n $IP
145+ else
146+ echo -n "$RTN"
147+ fi
148+}
149+
150+reverse_dns()
151+{
152+ local IP=$1
153+
154+ # Skip if it is not an IP
155+ if ! echo $IP | grep -q "^$IP_regex$"; then
156+ echo -n $IP
157+ return
158+ fi
159+
160+ # Do a dns lookup, if it is a local IP
161+ if echo $IP | grep -q $IP_private; then
162+ dns_lookup $IP
163+ return
164 fi
165+
166+ local NAME="$(get_val $IP)"
167+
168+ if [ -z "$NAME" ]; then
169+ echo -n $IP
170+ else
171+ echo -n $NAME
172+ fi
173+}
174+
175+#######################################################################
176+# Main
177+create_lookup_table
178+
179+while read LINE; do
180+ for IP in $(echo "$LINE" | \
181+ sed "s#\b\($IP_regex\)\b#\n\1\n#g" | \
182+ grep $IP_regex)
183+ do
184+ NAME=`reverse_dns $IP `
185+ # echo "$NAME $IP"
186+ LINE=`echo "$LINE" | sed "s#$IP#$NAME#" `
187+ done
188 echo $LINE
189-done < /dev/stdin
190+done
191
192--
1932.1.0
194