| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | package Daemon::Whois; | ||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use base qw(Net::Server); | ||
| 7 | use CDB_File; | ||
| 8 | |||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Daemon::Whois - A WHOIS daemon | ||
| 13 | |||
| 14 | =head1 VERSION | ||
| 15 | |||
| 16 | Version 1.11 | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | our $VERSION = '1.11'; | ||
| 21 | |||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | Quick summary of what the module does. | ||
| 26 | |||
| 27 | Perhaps a little code snippet. | ||
| 28 | |||
| 29 | use Daemon::Whois; | ||
| 30 | |||
| 31 | my $foo = Daemon::Whois->new(); | ||
| 32 | ... | ||
| 33 | |||
| 34 | =head1 VARIABLES | ||
| 35 | |||
| 36 | $database - The location of the database | ||
| 37 | $log_file - The location of the log file | ||
| 38 | $no_db - The error message when there is no database | ||
| 39 | $copyright- The footnote | ||
| 40 | $no_domain_found - Another error condition | ||
| 41 | $string_replace - Improving the error message | ||
| 42 | $user - The user that the daemon runs as | ||
| 43 | $group - The group the daemon runs as | ||
| 44 | $proto - You don't want to change this | ||
| 45 | $port - Not a WHOIS server if it is not port 43 | ||
| 46 | $template - A suggested layout | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | $|=1; | ||
| 51 | my %opt; #options | ||
| 52 | $opt{D}=0; #debug level | ||
| 53 | my $database = '/var/lib/whois/whois.cdb'; | ||
| 54 | my $log_file = '/var/log/whois.log'; | ||
| 55 | my $no_db =qq |The database is being updated, please try again later\n(You could poke the sysadmin if this has been down for too long, but please be gentel)\n|; | ||
| 56 | my $copyright =qq |This is meant to be informative and give an approximate guide... and nothing more.|; | ||
| 57 | my $no_domain_found =qq |That domain is not found|; | ||
| 58 | my $string_replace =qq |That domain|; #if we think that we have a domain we can s/// using this string | ||
| 59 | my $user = 'whois'; | ||
| 60 | my $group= 'whois'; | ||
| 61 | my $proto= 'tcp'; | ||
| 62 | my $port = 43; | ||
| 63 | my $template = ' | ||
| 64 | Domain name: | ||
| 65 | DOMAIN | ||
| 66 | |||
| 67 | Registrant: | ||
| 68 | <private> | ||
| 69 | |||
| 70 | Registrant type: | ||
| 71 | UK Individual | ||
| 72 | |||
| 73 | Registrant\'s address: | ||
| 74 | The registrant is a non-trading individual who has opted to have their | ||
| 75 | address omitted from the WHOIS service. | ||
| 76 | |||
| 77 | Registrar: | ||
| 78 | That would be registry.alexx.net | ||
| 79 | URL: http://registry.alexx.net | ||
| 80 | |||
| 81 | Relevant dates: | ||
| 82 | Registered on: 06-Jun-2XXX (Why does that matter?) | ||
| 83 | Renewal date: 06-Jun-2XXX (Private) | ||
| 84 | Last updated: 30-Jul-2XXX (None of your bees wax) | ||
| 85 | |||
| 86 | Registration status: | ||
| 87 | Registered until renewal date. | ||
| 88 | |||
| 89 | Name servers: | ||
| 90 | ns0.not.telling.you | ||
| 91 | ns1.do.i.look.like.a.dns.server.2.you | ||
| 92 | ns2.did.not.think.so | ||
| 93 | |||
| 94 | WHOIS rebuilt NOW'; | ||
| 95 | |||
| 96 | my $original_template =qq | | ||
| 97 | Domain name: DOMAIN | ||
| 98 | Registrant : REGISTRANT | ||
| 99 | Contact : { | ||
| 100 | CONTACT | ||
| 101 | } | ||
| 102 | Dates: { | ||
| 103 | Registered on: REGISTERED | ||
| 104 | Renewal date: RENEWAL | ||
| 105 | Last updated: UPDATE | ||
| 106 | } | ||
| 107 | Status : STATUS | ||
| 108 | Name servers: { | ||
| 109 | NS | ||
| 110 | } | ||
| 111 | |||
| 112 | WHOIS lookup made at QUERY_DATE | ||
| 113 | WHOIS database rebuilt DB_DATE | ||
| 114 | |||
| 115 | -- | ||
| 116 | COPYRIGHT | ||
| 117 | |; | ||
| 118 | |||
| 119 | =head1 METHOD | ||
| 120 | |||
| 121 | If called with a file location that includes the database name this | ||
| 122 | script will build an example database. (In the original version _build | ||
| 123 | accessed the database using Notice::DB Config::Auto DBIx::Class to | ||
| 124 | collect the data and CDB_File to build the whois.cdb file.) | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | if($ARGV[0]){ | ||
| 129 | if($database=~m/$ARGV[0]/){ | ||
| 130 | &_build($ARGV[0]); | ||
| 131 | }else{ print "This is not a valid output for the database\n Try perldoc Daemon::Whois"; } | ||
| 132 | exit; | ||
| 133 | } | ||
| 134 | |||
| 135 | |||
| 136 | =head1 SERVER | ||
| 137 | |||
| 138 | The server is called if invoked without any arguments. | ||
| 139 | The log output is disabled just to pass the make test; Once you create | ||
| 140 | the logfile with the user:group specified above, you can enable it. | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | my $daemon = Daemon::Whois->new({ | ||
| 145 | user => $user, | ||
| 146 | group => $group, | ||
| 147 | proto => $proto, | ||
| 148 | port => $port, | ||
| 149 | background=>1, | ||
| 150 | # log_file=> $log_file, | ||
| 151 | }); | ||
| 152 | |||
| 153 | $daemon->run; | ||
| 154 | |||
| 155 | =head1 process_request | ||
| 156 | |||
| 157 | The real meat of the server | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | sub process_request { | ||
| 162 | my $self = shift; | ||
| 163 | eval { | ||
| 164 | local $SIG{'ALRM'} = sub { die "Timed Out!\n" }; | ||
| 165 | my $timeout = 30; # give the user 30 seconds to type some lines | ||
| 166 | my $previous_alarm = alarm($timeout); | ||
| 167 | warn ("HERE: $previous_alarm") if $opt{"D"}>=2; | ||
| 168 | while (my $query = <STDIN>) { | ||
| 169 | last if $query =~ /^\s$/; | ||
| 170 | chomp $query; | ||
| 171 | my $domain = $query; | ||
| 172 | $domain=~s/\s.*//; | ||
| 173 | $domain=~s/[^\w\.-]//; | ||
| 174 | if(-f "$database"){ | ||
| 175 | tie my %whoisdb, 'CDB_File', $database or die "can't open $database: $!\n"; | ||
| 176 | warn "tieing to $database" if $opt{D}>=2; | ||
| 177 | my $now = `date +%Y%m%d%H%M%S`; chomp($now); | ||
| 178 | if ($whoisdb{$domain}){ | ||
| 179 | my $reply = $whoisdb{$domain}; | ||
| 180 | $reply=~s/\$now/$now/; | ||
| 181 | $reply=~s/QUERY_DATE/$now/; | ||
| 182 | print $reply . "\n"; | ||
| 183 | print STDERR "$domain is known\n" if $opt{D}>=1; #ths is a log message | ||
| 184 | }else{ | ||
| 185 | if('bandwidth to burn' eq 'yes'){ | ||
| 186 | my $reply = $no_domain_found; | ||
| 187 | $reply=~s/$string_replace/$domain/; | ||
| 188 | print $reply . "\nQuery recieved: $now\n"; | ||
| 189 | }else{ | ||
| 190 | print "No\n"; | ||
| 191 | } | ||
| 192 | # NOTE probably need a rate-limiter to prevent a DDoS filling up the logs. | ||
| 193 | print STDERR "$now $domain is not known here \n" if $opt{D}>=1; | ||
| 194 | } | ||
| 195 | last; | ||
| 196 | warn ("THERE: $timeout"); | ||
| 197 | }else{ | ||
| 198 | print $no_db; | ||
| 199 | print STDERR "DB ERROR: $database is not a file\n"; | ||
| 200 | last; | ||
| 201 | } | ||
| 202 | alarm($timeout); | ||
| 203 | } | ||
| 204 | alarm($previous_alarm); | ||
| 205 | if ($@=~m/timed out/i) { print STDERR "DIED Out.\n"; print STDERR "We have zero bananas today\n"; return; } | ||
| 206 | }; | ||
| 207 | if ($@=~m/timed out/i) { print STDERR "Timed Out.\n"; print STDERR "We have no bananas today\n"; return; } | ||
| 208 | elsif ($@) { print STDERR "NB: $@.\n"; return; } | ||
| 209 | else{ print STDERR "end of process\n" if $opt{D}>=2; } | ||
| 210 | } | ||
| 211 | |||
| 212 | |||
| 213 | |||
| 214 | =head1 SUBROUTINES/METHODS | ||
| 215 | |||
| 216 | =head2 _build | ||
| 217 | |||
| 218 | This will build an example database. | ||
| 219 | |||
| 220 | =cut | ||
| 221 | |||
| 222 | sub _build { | ||
| 223 | use DateTime; | ||
| 224 | my $t = new CDB_File ($ARGV[0], "t.$$") or die "can't create file $ARGV[0]"; | ||
| 225 | my @domains = ('test.example.com','this.example.com','example.com','alexx.net'); | ||
| 226 | foreach my $d (@domains){ | ||
| 227 | my $v = $template; | ||
| 228 | if($v=~s/DOMAIN/$d/){ | ||
| 229 | my $now = DateTime->now(); | ||
| 230 | $v=~s/NOW/$now/; | ||
| 231 | $t->insert($d,$v); | ||
| 232 | } | ||
| 233 | } | ||
| 234 | $t->finish; | ||
| 235 | # rename("t.$$",$database); | ||
| 236 | |||
| 237 | } | ||
| 238 | |||
| 239 | =head1 AUTHOR | ||
| 240 | |||
| 241 | Alexx Roche, C<< <notice-dev at alexx.net> >> | ||
| 242 | |||
| 243 | =head1 BUGS | ||
| 244 | |||
| 245 | Please report any bugs or feature requests to C<bug-daemon-whois at rt.cpan.org>, or through | ||
| 246 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Daemon-Whois>. I will be notified, and then you'll | ||
| 247 | automatically be notified of progress on your bug as I make changes. | ||
| 248 | |||
| 249 | |||
| 250 | |||
| 251 | |||
| 252 | =head1 SUPPORT | ||
| 253 | |||
| 254 | You can find documentation for this module with the perldoc command. | ||
| 255 | |||
| 256 | perldoc Daemon::Whois | ||
| 257 | |||
| 258 | |||
| 259 | You can also look for information at: | ||
| 260 | |||
| 261 | =over 4 | ||
| 262 | |||
| 263 | =item * RT: CPAN's request tracker | ||
| 264 | |||
| 265 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Daemon-Whois> | ||
| 266 | |||
| 267 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 268 | |||
| 269 | L<http://annocpan.org/dist/Daemon-Whois> | ||
| 270 | |||
| 271 | =item * CPAN Ratings | ||
| 272 | |||
| 273 | L<http://cpanratings.perl.org/d/Daemon-Whois> | ||
| 274 | |||
| 275 | =item * Search CPAN | ||
| 276 | |||
| 277 | L<http://search.cpan.org/dist/Daemon-Whois/> | ||
| 278 | |||
| 279 | =back | ||
| 280 | |||
| 281 | |||
| 282 | =head1 ACKNOWLEDGEMENTS | ||
| 283 | |||
| 284 | |||
| 285 | =head1 LICENSE AND COPYRIGHT | ||
| 286 | |||
| 287 | Copyright (C) 2011 Alexx Roche | ||
| 288 | |||
| 289 | This program is free software; you can redistribute it and/or modify it | ||
| 290 | under the Eclipse Public License, Version 1.0 | ||
| 291 | |||
| 292 | See http://www.opensource.org/licenses/ for more information. | ||
| 293 | |||
| 294 | This program is distributed in the hope that it will be useful, | ||
| 295 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 296 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| 297 | |||
| 298 | =cut | ||
| 299 | |||
| 300 | 1; # End of Daemon::Whois |