2 /* dnslookup.c - NeonServ v5.6
3 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 function time2str($time, $anz = 9) {
22 if($time>=(60*60*24*365) && $anz > 0) {
24 $years=floor($time/(60*60*24*365));
26 $time=$time-((60*60*24*365)*$years);
28 if($time>=(60*60*24*30) && $anz > 0) {
30 $months=floor($time/(60*60*24*30));
31 if($str != "") $str.=" ";
33 $time=$time-((60*60*24*30)*$months);
35 if($time>=(60*60*24) && $anz > 0) {
37 $days=floor($time/(60*60*24));
38 if($str != "") $str.=" ";
40 $time=$time-((60*60*24)*$days);
42 if($time>=(60*60) && $anz > 0) {
44 $stunden=floor($time/(60*60));
45 if($str != "") $str.=" ";
47 $time=$time-((60*60)*$stunden);
49 if($time>=(60) && $anz > 0) {
51 $min=floor($time/(60));
52 if($str != "") $str.=" ";
54 $time=$time-((60)*$min);
56 if(($time>1 || $str == "") && $anz > 0){
58 if($str != "") $str.=" ";
66 $show_record = strtoupper($argv[2]);
68 $pattern_ipv6 = '/^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))(|\/[0-9]{1,3})$/';
69 $pattern_ipv4 = '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(|\/[0-9]{1,2})$/';
71 $record_order = array("SOA", "NS", "A", "AAAA", "MX");
73 function show_subrecords($host) {
74 $dns = dns_get_record($host, DNS_ALL);
76 usort($dns, "sort_records");
77 foreach($dns as $record) {
78 switch($record['type']) {
80 echo" \002A \002 ".$record['ip'].($record['ttl'] < 86400 ? " (ttl: ".time2str($record['ttl'],2).")" : "")."\n";
83 echo" \002AAAA \002 ".$record['ipv6'].($record['ttl'] < 86400 ? " (ttl: ".time2str($record['ttl'],2).")" : "")."\n";
86 echo" \002CNAME \002 ".$record['target']."\n";
93 function sort_records($a, $b) {
95 $index_a = array_search($a['type'], $record_order);
96 if($index_a === FALSE) $index_a = count($record_order);
97 $index_b = array_search($b['type'], $record_order);
98 if($index_b === FALSE) $index_b = count($record_order);
99 $order = $index_a - $index_b;
101 switch($record['type']) {
112 $suborder = "target";
115 $order = strcmp($a[$suborder], $b[$suborder]);
120 if(strlen($host) && preg_match("#^((([a-z0-9-.]*)\.|)([a-z]{2,5})|).?$#i", $host)) {
121 $dns = dns_get_record($host, DNS_ALL);
122 echo"DNS Records for \002".$host."\002:";
123 if($show_record != "" && $show_record != "*" && $show_record != "ANY")
124 echo" (".$show_record.")";
127 usort($dns, "sort_records");
128 foreach($dns as $record) {
129 if($show_record != "" && $show_record != "*" && $show_record != "ANY" && $show_record != $record['type'])
131 switch($record['type']) {
133 echo" \002A \002 ".$record['ip'].($record['ttl'] < 86400 ? " (ttl: ".time2str($record['ttl'],2).")" : "")."\n";
136 echo" \002AAAA \002 ".$record['ipv6'].($record['ttl'] < 86400 ? " (ttl: ".time2str($record['ttl'],2).")" : "")."\n";
139 echo" \002MX \002 ".$record['target']." (priority: ".$record['pri'].")\n";
140 if($show_record == "MX") {
141 show_subrecords($record['target']);
145 echo" \002NS \002 ".$record['target']."\n";
146 if($show_record == "NS") {
147 show_subrecords($record['target']);
151 echo" \002CNAME \002 ".$record['target']."\n";
154 echo" \002TXT \002 ".$record['txt']."\n";
157 if($show_record != "SOA") {
158 echo" \002SOA \002 (see: dns ".$host." SOA)\n";
161 echo" \002SOA \002 (Start of Authority):\n";
162 echo" name: ".$record['mname']."\n";
163 echo" serial: ".$record['serial']."\n";
164 echo" refresh: ".$record['refresh']." (".time2str($record['refresh'], 2).")\n";
165 echo" retry: ".$record['retry']." (".time2str($record['retry'], 2).")\n";
166 echo" expire: ".$record['expire']." (".time2str($record['expire'], 2).")\n";
167 echo" TTL: ".$record['minimum-ttl']." (".time2str($record['minimum-ttl'], 2).")\n";
172 echo"No records found.\n";
173 } else if(preg_match($pattern_ipv4, $host) || preg_match($pattern_ipv6, $host)) {
174 $hostname = gethostbyaddr($host);
175 echo"Reverse Lookup for \002".$host."\002:\n";
176 echo " \002PTR \002 ".$hostname."\n";
178 echo"Invalid Hostname or IP-Address.\n";