File: dnslookup.pl

package info (click to toggle)
libnet-dns-perl 0.19-0.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 544 kB
  • ctags: 323
  • sloc: perl: 5,191; makefile: 54
file content (356 lines) | stat: -rwxr-xr-x 9,748 bytes parent folder | download
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
#!/usr/bin/perl -w
#
# $Id: dnslookup.cgi,v 2.1 1997/10/22 21:18:56 skh Exp $
#
# This script is a CGI interface to the domain name system.  This version
# of the script requires that you have the Net::DNS module from CPAN installed
# and be running perl5.003 or higher.
#
# If you have suggestions or improvements feel free to send 'em to me.
#
# Kent Hamilton
#   Work:  <KHamilton@Hunter.COM> 
#   Home:  <KentH@HNS.St-Louis.Mo.US>
#    URL:  http://www2.hunter.com/~skh/
#

$| = 0;

use File::Basename;
use Net::DNS;

####################################
# Set your defaults here folks.
####################################
$DEBUG = 0;
$nameserver = "ns.hunter.com";
$lookuptype = "name_info";
$lookup = "";
$formurl = "/~skh/scripts/dnslookup.html";
$uparrowurl = "/~skh/images/up.gif";

#
# Get our input from the HTML form....
#
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

@pairs = split(/&/, $buffer);

foreach $pair (@pairs) {
    # Set this to the characters valid for your input.  Try
    # not to use anything toooo nasty.  (IE `.<>; etc)
    $ok_chars = "a-zA-Z0-9_\-\.";
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $value =~ s/<([^>]|\n)*>//g;
    $value =~ s/<//g;
    $value =~ s/>//g;
    $value =~ s/`;/_/g;
# Strip out everything not an allowed character (translate it to a '_').
    $value =~ eval "tr/[$ok_chars]/_/c";
    $FORM{$name} = $value;
}

#
# Init our Resolver info.
# If they entered a name server then use it.
#
my $res = new Net::DNS::Resolver;

if ($FORM{'n_server'}) {
    my $nameserver = $FORM{'n_server'};
    $res->nameservers($nameserver);
}

#
# Some default values.
#
$lookup = $FORM{'lookup_val'};
$lookuptype = $FORM{'choice'};
$type  ||= "A";
$class ||= "IN";

#
# They didn't enter anything to search for so yell at 'em.
#
if ( $lookup eq "" ) {
    print "Content-type: text/html\n\n";
    print "<HTML>\n";
    print "<HEAD>\n";
    print "<TITLE>DNS Lookup Error!</TITLE>\n";
    print "</HEAD>\n";
    print "<BODY BGCOLOR=\"#ffffff\">\n";
    print "<CENTER><P><BR><BR><BR>\n";
    print "You must enter at least a name or IP address to search for.<BR>\n";
    print "<P><BR><BR>\n";
    print "Return to the <A HREF=\"$formurl\">DNS Lookup</A> form.\n";
    print "</BODY></HTML>\n";
    exit;
}

#
# Set the type of lookup to do.
# Some of these get reset below but....
#
if ($lookuptype eq 'name_info' ) {
    $type = "ANY";
    $qstring = 'any information';
} elsif ($lookuptype eq 'ns_lookup') {
    $type = "NS";
    $qstring = 'name server(s)';
} elsif ($lookuptype eq 'domain_list') {
    $type = "AXFR";
    $qstring = 'a zone listing';
} elsif ($lookuptype eq 'soa') {
    $type = "SOA";
    $qstring = 'the start of authority information';
} elsif ($lookuptype eq 'subnet_list') {
    $type = "AXFR";
    $qstring = 'a subnet listing';
    if ( $lookup =~ m/^\d+(\.\d+){0,3}$/ ) {
        $reverse = join ('.', reverse (split ('\.', $lookup ))) . '.in-addr.arpa';
        $lookup = $reverse;
    }
} elsif ($lookuptype eq 'mail_exch') {
    $type = "MX";
    $qstring = 'the mail exchangers';
} else {
    $type = "ANY";
    $qstring = 'any information';
}

if ($DEBUG) {
    print "Content-type: text/html\n\n";
    print "<HTML>\n";
    print "<HEAD>\n";
    print "<TITLE>DNS Lookup Debug!</TITLE>\n";
    print "</HEAD>\n";
    print "<BODY BGCOLOR=\"#ffffff\">\n";
    print "<CENTER><P><BR><BR><BR>\n";
    print "lookup = $lookup<BR>\n";
    print "lookuptype = $lookuptype<BR>\n";
    print "nameserver = $nameserver<BR>\n";
    print "FORM('choice') = $FORM{'choice'}<BR>\n";
    print "FORM('n_server') = $FORM{'n_server'}<BR>\n";
    print "FORM('lookup_val') = $FORM{'lookup_val'}<BR>\n";
}

#
# Do a zone transfer for either a forward or reverse zone.
#
if ($lookuptype eq "domain_list" || $lookuptype eq "subnet_list" ) {
    my @zone = $res->axfr($lookup, $class);
        if (defined(@zone) && @zone) {
            &html_header;
            &html_question($qstring, $lookup, $nameserver);
            print "<TABLE>\n";
            foreach (@zone) {
                $_->print;
                print "<BR>\n";
            }
            &html_footer;
        } else {
            &html_header;
            &html_queryerr("Zone transfer failed: ", $res->errorstring);
            &html_footer;
            exit;
        }
} else {
#
# This is the "anything else" case.
#
    my $packet = $res->send($lookup, $type, $class);
    if (defined($packet)) {
        &html_header;
        &html_question( $qstring, $lookup, $nameserver);
        if (! $packet->header->aa) {
            print "<P><BR><B>Non-authoritative answer</B><BR><BR>\n";
        }
        $anscount = $packet->header->ancount;
        if ( $anscount gt 0 ) {
            flush;
            @answer = $packet->answer;
            print "<TABLE>\n";
            print "<TR>\n";
            print "<TH>Search String";
            print "<TH>Result Type";
            print "<TH>Result Data";
            print "<TH> </TH>";
            print "</TR>\n";
            foreach $rr ( @answer ) {
                &print_record( $rr );
            }
            print "</TABLE>";
            print "</CENTER>";
        } else {
            &html_noresponse;
            exit;
        }
        &html_footer;
    } else {
        &html_header;
        &html_queryerr("Query failed: ", $res->errorstring);
        &html_footer;
        exit;
    }
}
exit;

sub html_header {
    print "Content-type: text/html\n\n";
    print "<HTML>\n<HEAD>\n";
    print "<TITLE>Domain Name Server Query Results</TITLE>\n";
    print "<LINK REL=STYLESHEET TYPE=\"text/css\" HREF=\"/stylesheets/standard.html\">";
    print "</HEAD>\n<BODY BGCOLOR=\"#ffffff\">\n\n";
    print "<CENTER>";
    print "<H1 CLASS=green>Domain Name Server Query Results</H1>\n";
    print "</CENTER>";
    print "<P><HR NOSHADE>\n";
    print "<FONT FACE=\"Arial\" SIZE=\"2\" COLOR=\"blue\">";
}


sub html_footer {
    print "<P>\n<HR NOSHADE>\n<P>\n";
    print "<A HREF=\"$formurl\">";
    print "<IMG SRC=\"$uparrowurl\"> Return to the DNS Form</A>\n";
    print "<P>\n</BODY>\n</HTML>\n";
}

sub html_question {
    my $qstring = shift;
    my $lookup = shift;
    my $nameserver = shift;
    print "<BR>\n";
    print "The question you asked was:<BR>\n";
    printf "Lookup %s for %s at %s.<BR>\n", $qstring, $lookup, $nameserver;
    print "<BR><HR NOSHADE><BR>\n";
}

sub html_queryerr {
    my $errstr = shift;
    my $resstr = shift;
    printf "<FONT FACE=\"Arial\" SIZE=\"2\" COLOR=\"blue\">";
    printf "<CENTER>\n";
    printf "<BR>%s %s<BR>\n", $errstr, $resstr;
    printf "<P></CENTER>\n<HR NOSHADE>\n<P>\n";
    printf "<A HREF=\"$formurl\">";
    printf "<IMG SRC=\"$uparrowurl\"> Return to the DNS Form</A>\n";
    printf "<P>\n</BODY>\n</HTML>\n";
    exit;
}

sub html_noresponse {
    print "<FONT FACE=\"Arial\" SIZE=\"2\" COLOR=\"blue\">";
    print "<CENTER>\n";
    print "<BR>No results were returned for this query<BR>\n";
    print "<P></CENTER>\n<HR NOSHADE>\n<P>\n";
    print "<A HREF=\"$formurl\">";
    print "<IMG SRC=\"$uparrowurl\"> Return to the DNS Form</A>\n";
    print "<P>\n</BODY>\n</HTML>\n";
    exit;
}

sub print_record {
    my $rr = shift;

    if ( $rr->type eq "MX" ) {
        print "<TR>\n";
        &print_mx($rr);
        print "</TR>\n";
    } elsif ( $rr->type eq "SOA") {
        print "<TR>\n";
        &print_soa($rr);
        print "</TR>\n";
    } elsif ( $rr->type eq "NS") {
        print "<TR>\n";
        &print_ns($rr);
        print "</TR>\n";
    } elsif ( $rr->type eq "HINFO") {
        print "<TR>\n";
        &print_hinfo($rr);
        print "</TR>\n";
    } else {
        print "<TR>\n";
        &print_any($rr);
        print "</TR>\n";
    }
}

sub print_soa {
    my $soa = shift;
    printf "<TABLE>\n";
    printf "<TR>\n";
    printf "<TD>  </TD>";
    printf "<TD>Authoritative Server:</TD>";
    printf "<TD>%s</TD>",$soa->mname;
    printf "<TR>\n";
    printf "<TD>   </TD>";
    printf "<TD>  Responsible Person:</TD>";
    printf "<TD>%s</TD>",$soa->rname;
    printf "<TR>\n";
    printf "<TD>   </TD>";
    printf "<TD>  Zone Serial Number:</TD>\n";
    printf "<TD>%s</TD>",$soa->serial;
    printf "<TR>\n";
    printf "<TD>   </TD>";
    printf "<TD>    Refresh Interval:</TD>\n";
    printf "<TD>%s</TD>",$soa->refresh;
    printf "<TR>\n";
    printf "<TD>   </TD>";
    printf "<TD>      Retry Interval:</TD>\n";
    printf "<TD>%s</TD>",$soa->retry;
    printf "<TR>\n";
    printf "<TD>   </TD>";
    printf "<TD>     Expire Interval:</TD>\n";
    printf "<TD>%s</TD>",$soa->expire;
    printf "<TR>\n";
    printf "<TD>   </TD>";
    printf "<TD>Minimum Time to Live:</TD>\n";
    printf "<TD>%s</TD>",$soa->minimum;
    printf "</TR>\n";
}

sub print_mx {
    my $mx = shift;

    printf "<TR>\n";
    printf "<TD>%s</TD>\n", $mx->name;
    printf "<TD>%s</TD>\n", $mx->type;
    printf "<TD>%s</TD>\n", $mx->preference;
    printf "<TD>%s</TD>\n", $mx->exchange;
    printf "</TR>\n";
}

sub print_ns {
    my $ns = shift;
  
    printf "<TR>\n";
    printf "<TD>%s</TD>\n", $ns->name;
    printf "<TD>%s</TD>\n", $ns->type;
    printf "<TD>%s</TD>\n", $ns->nsdname;
    printf "</TR>\n";
}

sub print_hinfo {
    my $hinfo = shift;

    printf "<TR>\n";
    printf "<TD>%s</TD>\n", $hinfo->name;
    printf "<TD>%s</TD>\n", $hinfo->type;
    printf "<TD>CPU: %s</TD>\n", $hinfo->cpu;
    printf "<TD>O/S: %s</TD>\n", $hinfo->os;
    printf "</TR>\n";
}

sub print_any {
    my $rr = shift;

    printf "<TR>\n";
    printf "<TD>%s</TD>\n", $rr->name;
    printf "<TD>%s</TD>\n", $rr->type;
    printf "<TD>%s</TD>\n", $rr->rdatastr;
    printf "</TR>\n";
}